Skip to content

Instantly share code, notes, and snippets.

@Melvillian
Last active August 12, 2023 12:05
Show Gist options
  • Save Melvillian/17d10138f616893eeb88525987c6f7d5 to your computer and use it in GitHub Desktop.
Save Melvillian/17d10138f616893eeb88525987c6f7d5 to your computer and use it in GitHub Desktop.
Solutions to "move_semantics2.rs" of Rustlings
/// 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
}
@presci
Copy link

presci commented Sep 16, 2020

`
fn main() {
let vec0 = Vec::new();

let mut vec1 = fill_vec(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: Vec) -> Vec {
let mut vec = vec;

vec.push(22);
vec.push(44);
vec.push(66);

vec

}
`

@lazywalker
Copy link

lazywalker commented Oct 17, 2020

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]`

====================

@Flimm
Copy link

Flimm commented Aug 12, 2023

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