Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use a reference instead of transferring ownership?

From the Rust book's chapter on ownership, non-copyable values can be passed to functions by either transferring ownership or by using a mutable or immutable reference. When you transfer ownership of a value, it can't be used in the original function anymore: you must return it back if you want to. When you pass a reference, you borrow the value and can still use it.

I come from languages where values are immutable by default (Haskell, Idris and the like). As such, I'd probably never think about using references at all. Having the same value in two places looks dangerous (or, at least, awkward) to me. Since references are a feature, there must be a reason to use them.

Are there situations I should force myself to use references? What are those situations and why are they beneficial? Or are they just for convenience and defaulting to passing ownership is fine?

like image 977
MaiaVictor Avatar asked Apr 25 '18 17:04

MaiaVictor


People also ask

How do references work in Rust?

In Rust, by contrast, the compiler guarantees that references will never be dangling references: if you have a reference to some data, the compiler will ensure that the data will not go out of scope before the reference to the data does.

Does Rust have references?

Rust supports mutable references which means we can change the value it references if it's a mutable variable. There are some restrictions on how we use mutable references. If the immutable reference exists in our program then We can't have a mutable reference into the same value.

What is a mutable reference?

A mutable type is a type whose instance data can be modified. The System. Text. StringBuilder class is an example of a mutable reference type. It contains members that can change the value of an instance of the class.

How do you pass a reference in Rust?

Rust allows you to pass a reference to the variable instead. This allows the parent function to keep the scope of the original variable whilst allowing a child function to use it. The clear difference in the code is the use of “&” when passing the variable to the function and in the function definition.


1 Answers

Mutable references in particular look very dangerous.

They are not dangerous, because the Rust compiler will not let you do anything dangerous. If you have a &mut reference to a value then you cannot simultaneously have any other references to it.

In general you should pass references around. This saves copying memory and should be the default thing you do, unless you have a good reason to do otherwise.

Some good reasons to transfer ownership instead:

  1. When the value's type is small in size, such as bool, u32, etc. It's often better performance to move/copy these values to avoid a level of indirection. Usually these values implement Copy, and actually the compiler may make this optimisation for you automatically. Something it's free to do because of a strong type system and immutability by default!
  2. When the value's current owner is going to go out of scope, you may want to move the value somewhere else to keep it alive.
like image 68
Peter Hall Avatar answered Sep 19 '22 21:09

Peter Hall