Copy generictype + trait implementation for array needed in a separate function

fn foo<A>(a: A) {}        // Passed by value; copied if A: Copy, else moved
fn foo1<A>(a: &A) {}      // Passed by immutable (shared) reference
fn foo2<A>(a: &mut A) {}  // Passed by mutable (unique) reference

The compiler is free to make any optimizations that preserve the semantics. But the above are the semantics that each of these types of argument passing have. If you're interested in what it actually compiles down to, https://play.rust-lang.org/ and http://rust.godbolt.org/ can help you see what some given code actually optimizes down into; the latter provides some nice visualization since it uses debug information to highlight lines corresponding the original source, though this actually disables some optimizations so you may need to turn it off if you want to see the fully optimized code.

1 Like