Last active
August 12, 2023 12:05
-
-
Save Melvillian/17d10138f616893eeb88525987c6f7d5 to your computer and use it in GitHub Desktop.
Solutions to "move_semantics2.rs" of Rustlings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// given this uncompilable code | |
pub fn main() { | |
let vec0 = Vec::new(); | |
let mut vec1 = fill_vec(vec0); | |
// Do not change the following line! | |
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); | |
vec1.push(88); | |
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); | |
} | |
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { | |
let mut vec = vec; | |
vec.push(22); | |
vec.push(44); | |
vec.push(66); | |
vec | |
} | |
/// the solutions are: | |
/// make another Vec to use in fill_vec | |
pub fn main() { | |
let vec0 : Vec<i32> = Vec::new(); | |
let vec2 = Vec::new(); | |
let mut vec1 = fill_vec(vec2); | |
// Do not change the following line! | |
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); | |
vec1.push(88); | |
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); | |
} | |
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { | |
let mut vec = vec; | |
vec.push(22); | |
vec.push(44); | |
vec.push(66); | |
vec | |
} | |
/// borrowing mutably | |
pub fn main() { | |
let mut vec0 = Vec::new(); | |
fill_vec(&mut vec0); | |
// Do not change the following line! | |
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); | |
} | |
fn fill_vec(vec: &mut Vec<i32>) -> Vec<i32> { | |
vec.push(22); | |
vec.push(44); | |
vec.push(66); | |
vec.to_vec() | |
} | |
// simply borrowing | |
pub fn main() { | |
let vec0 = Vec::new(); | |
let mut vec1 = fill_vec(&vec0); | |
// Do not change the following line! | |
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); | |
vec1.push(88); | |
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); | |
} | |
fn fill_vec(vec:& Vec<i32>) -> Vec<i32> { | |
let mut vec = vec.clone(); | |
vec.push(22); | |
vec.push(44); | |
vec.push(66); | |
vec | |
} |
fn main() {
let mut vec0 = Vec::new();
fill_vec(&mut vec0);
let mut vec1 = vec0.clone();
// Do not change the following line!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
vec1.push(88);
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}
fn fill_vec(vec: &mut Vec<i32>) {
vec.push(22);
vec.push(44);
vec.push(66);
}
- output:
Output:
====================
vec0 has length 3 content `[22, 44, 66]`
vec1 has length 4 content `[22, 44, 66, 88]`
====================
According to the comments at the top of the file, this is the expected output:
// Expected output:
// vec0 has length 3 content `[22, 44, 66]`
// vec1 has length 4 content `[22, 44, 66, 88]`
None of these solutions produce the expected output. See rust-lang/rustlings#1596
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`
fn main() {
let vec0 = Vec::new();
}
fn fill_vec(vec: Vec) -> Vec {
let mut vec = vec;
}
`