Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass an array to a function in Rust and change its content?

Tags:

arrays

rust

I want to pass an array to a function and change the content inside it:

fn change_value(mut arr: &[i32]) {     arr[1] = 10; }  fn main() {     let mut arr: [i32; 4] = [1, 2, 3, 4];     change_value(&arr);     println!("this is {}", arr[1]); } 

I'm getting this error:

warning: variable does not need to be mutable  --> src/main.rs:2:17   | 2 | fn change_value(mut arr: &[i32]) {   |                 ----^^^   |                 |   |                 help: remove this `mut`   |   = note: `#[warn(unused_mut)]` on by default  error[E0594]: cannot assign to `arr[_]` which is behind a `&` reference  --> src/main.rs:3:5   | 2 | fn change_value(mut arr: &[i32]) {   |                          ------ help: consider changing this to be a mutable reference: `&mut [i32]` 3 |     arr[1] = 10;   |     ^^^^^^^^^^^ `arr` is a `&` reference, so the data it refers to cannot be written  warning: variable does not need to be mutable  --> src/main.rs:7:9   | 7 |     let mut arr: [i32; 4] = [1, 2, 3, 4];   |         ----^^^   |         |   |         help: remove this `mut` 

I've been searching around, but I can't find anything.

like image 265
chapman Avatar asked Jul 18 '14 18:07

chapman


People also ask

Are arrays mutable in Rust?

Like many programming languages, Rust has list types to represent a sequence of things. The most basic is the array, a fixed-size list of elements of the same type. By default, arrays are immutable. Arrays have type [T; N] .

How do you print an array in Rust?

To print an array in Rust, we use the 😕 Operator inside the println! function.

How do I return an array in Rust?

The GetArray() function is a user-defined function that returns the array to the calling function. In the main() function, we called GetArray() function to get the array of integers. Then we printed the returned array.

Are there arrays in Rust?

Arrays are collections of same-type-data values stored in contiguous memory locations. In Rust, arrays are created using square brackets [] and their size needs to be known at compile time. An array whose size is not defined is called a slice.


1 Answers

Rust references (denoted by the & sign) are of two kinds: immutable (&T) and mutable (&mut T). In order to change the value behind the reference, this reference has to be mutable, so you need to:

  1. accept &mut [i32] as the function argument, not &[i32]
  2. pass &mut arr to the function, not &arr:
fn change_value(arr: &mut [i32]) {     arr[1] = 10; }  fn main() {     let mut arr: [i32; 4] = [1, 2, 3, 4];     change_value(&mut arr);     println!("this is {}", arr[1]); } 

You don't need mut arr in change_value's argument because mut there denotes mutability of that variable, not of the data it points to. With mut arr: &[i32] you can reassign arr itself (for it to point to a different slice), but you can't change the data it references.

If you wanted to accept an array instead of a slice, you could also do that:

fn change_value(arr: &mut [i32; 4]) {     arr[1] = 10; } 

See also:

  • What's the difference between placing "mut" before a variable name and after the ":"?
like image 86
Vladimir Matveev Avatar answered Oct 09 '22 04:10

Vladimir Matveev