Rustlings: move_semantics1.rs #Issue23 — Move Semantics in Rust
Rustlings Challenge: move_semantics1.rs Solution Walkthrough
Published in
3 min readSep 26, 2023
This is the twenty-third (23rd) issue of the Rustlings series. In this issue, we provide solutions to Rustlings exercises along with detailed explanations. In this issue we will solve the challenge on move_semantics1.rs.
Previous challenge #Issue 22
Challenge:
// move_semantics1.rs
//
// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand
// for a hint.
// I AM NOT DONE
#[test]
fn main() {
let vec0 = vec![22, 44, 66];
let vec1 = fill_vec(vec0);
assert_eq!(vec1, vec![22, 44, 66, 88]);
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let vec = vec;
vec.push(88);
vec
}
If you run the code on your editor, the compiler will throw the error:
Compiling playground v0.0.1 (/playground)
error[E0596]: cannot borrow `vec` as mutable, as it is not declared as mutable
-->…