-
Hello, I just completed The variable How does the function make the given I couldn't find an explanation and hope you can enlighten me. Best regards |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hello, Adding So you can think of: fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(88);
vec
} As: fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;
vec.push(88);
vec
} Without the ability to add |
Beta Was this translation helpful? Give feedback.
-
Thank you for the great explanation. :) |
Beta Was this translation helpful? Give feedback.
-
Just adding something to the great answer by @ProdOrDev
|
Beta Was this translation helpful? Give feedback.
Hello,
Adding
mut
in front of a function parameter makes the parameter (or more accurately variable) itself mutable within the function body, exactly like in a variable declaration.So you can think of:
As:
Without the ability to add
mut
to function parameters you would have to manually shadow them if you wanted to modify them, using thelet mut x = x
pattern.