Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between placing "mut" before a variable name and after the ":"?

Here are two function signatures I saw in the Rust documentation:

fn modify_foo(mut foo: Box<i32>) { *foo += 1; *foo } fn modify_foo(foo: &mut i32) { *foo += 1; *foo } 

Why the different placement of mut?

It seems that the first function could also be declared as

fn modify_foo(foo: mut Box<i32>) { /* ... */ } 
like image 698
Jimmy Lu Avatar asked Feb 18 '15 15:02

Jimmy Lu


People also ask

What does Mut mean Rust?

My current understanding is, that mut has two effects: It allows to assign another value to the variable later on. It allows to modify the object that the variable refers to later on.

What is a mutable reference?

A mutable reference is a borrow to any type mut T , allowing mutation of T through that reference. The below code illustrates the example of a mutable variable and then mutating its value through a mutable reference ref_i . fn main() {

How do you make a mutable variable?

Although variables are immutable by default, you can make them mutable by adding mut in front of the variable name as you did in Chapter 2. Adding mut also conveys intent to future readers of the code by indicating that other parts of the code will be changing this variable's value.

Why are Rust variables immutable by default?

There is no single reason that bindings are immutable by default, but we can think about it through one of Rust's primary focuses: safety. If you forget to say mut , the compiler will catch it, and let you know that you have mutated something you may not have intended to mutate.


1 Answers

If you're coming from C/C++, it might also be helpful to think of it basically like this:

// Rust          C/C++     a: &T     == const T* const a; // can't mutate either mut a: &T     == const T* a;       // can't mutate what is pointed to     a: &mut T == T* const a;       // can't mutate pointer mut a: &mut T == T* a;             // can mutate both 

You'll notice that these are inverses of each other. C/C++ take a "blacklist" approach, where if you want something to be immutable you have to say so explicitly, while Rust takes a "whitelist" approach, where if you want something to be mutable you have to say so explicitly.

like image 119
anderspitman Avatar answered Sep 24 '22 01:09

anderspitman