Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can immutable variables be passed as arguments to functions that require mutable arguments?

Example code:

fn main() {
    let a = [1, 2, 3, 4, 5];
    reset(a);
}

fn reset(mut b: [u32; 5]) {
    b[0] = 5;
}

The variable a is an immutable array, and the reset function's parameter b is a mutable array; intuitively I need to modify a to a mutable array before I can call the reset method, but the compiler tells me that I don't need to do this, why is this?

fn main() {
    let mut a = [1, 2, 3, 4, 5];
    reset(a);
}

fn reset(mut b: [u32; 5]) {
    b[0] = 5;
}
warning: variable does not need to be mutable
 --> src/main.rs:2:9
  |
2 |     let mut a = [1, 2, 3, 4, 5];
  |         ----^
  |         |
  |         help: remove this `mut`
  |
  = note: #[warn(unused_mut)] on by default
like image 761
iDuanYingJie Avatar asked Jan 10 '19 01:01

iDuanYingJie


People also ask

What is the difference between passing mutable type arguments and immutable type arguments to the function?

To summarise the difference, mutable objects can change their state or contents and immutable objects can't change their state or content. Immutable Objects : These are of in-built types like int, float, bool, string, unicode, tuple. In simple words, an immutable object can't be changed after it is created.

Can a mutable object be passed a parameter to a function?

If you pass a mutable object, like a list or a dictionary, it's like pass-by-reference. Again, you can't reassign the parameter to something else, but you can modify the object that you get.

Why are variables immutable in Python?

In python, some variables can change their value after creation while some cannot. Therefore, if a variable can change its value it is mutable in nature. Otherwise, if it cannot change its value after creation it is immutable in nature.

What is difference between mutable and immutable?

If the value can change, the object is called mutable, while if the value cannot change, the object is called immutable.


1 Answers

When you pass by value, you are transferring ownership of the value. No copies of the variable are required — first main owns it, then reset owns it, then it's gone1.

In Rust, when you have ownership of a variable, you can control the mutability of it. For example, you can do this:

let a = [1, 2, 3, 4, 5];
let mut b = a;

You could also do the same thing inside of reset, although I would not do this, preferring to use the mut in the function signature:

fn reset(b: [u32; 5]) {
    let mut c = b;
    c[0] = 5;
}

See also:

  • What's the idiomatic way to pass by mutable value?
  • What's the difference between placing "mut" before a variable name and after the ":"?

1 — In this specific case, your type is an [i32; 5], which implements the Copy trait. If you attempted to use a after giving ownership to reset, then an implicit copy would be made. The value of a would appear unchanged.

like image 99
Shepmaster Avatar answered Oct 02 '22 01:10

Shepmaster