I wanted to know if rust is doing variable shadowing under the hood when variables move from one scope to another.
Since when I move a variable to another function the other function's signature can be changed to make the variable mutable and but when the variable is moved/returned back from other function to main it's not immutable anymore.
I am just curious to what's happening here.
fn main() { let vec0 = Vec::new(); // immutable by default let mut vec1 = foreign_func(vec0); // vec0 has now moved // Is foreign_func returning a mutable vector? Because without `mut` keyword vec1 is immutable by default. vec1.push(10); } fn foreign_func(mut vec: Vec<i32>) -> Vec<i32> { // argument passed for vec is now mutable in this scope. // Is this like variable shadowing? // let test = 10; // let mut test = 20; vec.push(20); vec }
foreign_funcbeing able to mutatevecbut then whenvecis moved back tomainit's no more mutable. Sovecwas passed as immutable by rust internally?