Am I over-generalizing to say the following? These seem like good guidelines to me based on my limited experience so far in writing Rust code. While I know there are situations in which these do not apply,
it seems that following these typically reduces ownership issues in my code.
-
Collections should own their heap data rather than hold references to heap data owned elsewhere.
For example, struct fields that are strings should useString
instead of&str
. -
Pass references to heap data to functions rather than transferring ownership.
For example, a parameter that accepts string data should have the type&str
instead ofString
. -
In functions that create and return heap data, transfer ownership to the caller.
For example, returnString
rather than&str
.