mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-10 12:49:18 +00:00
commit
34d15b05d8
@ -2,30 +2,199 @@
|
||||
// Make me compile without changing line 13 or moving line 10!
|
||||
// Execute `rustlings hint move_semantics2` for hints :)
|
||||
|
||||
// I AM NOT DONE
|
||||
// // original code
|
||||
// fn main() {
|
||||
// // original code
|
||||
// // needed for solution0/solution1
|
||||
// // comment out for solution2
|
||||
// let vec0 = Vec::new();
|
||||
|
||||
// // solution3
|
||||
// // comment out for orignal/solution0/solution1/solution2
|
||||
// // create copy of vec0
|
||||
// let vec0_copy = vec0.clone();
|
||||
|
||||
// // solution2 (part0)
|
||||
// // comment out for orignal/solution0/solution1
|
||||
// // let mut vec0 = &mut Vec::new();
|
||||
|
||||
// // original code
|
||||
// // comment out for solution0/solution1/solution2/solution3
|
||||
// // let mut vec1 = fill_vec(vec0);
|
||||
|
||||
// // solution0 (part0)
|
||||
// // comment out for orignal/solution1/solution2
|
||||
// // let mut vec0_ref = &vec0;
|
||||
|
||||
// // solution0 (part1)
|
||||
// // comment out for orignal/solution1/solution2
|
||||
// // let mut vec1 = fill_vec(vec0_ref.to_vec());
|
||||
|
||||
// // solution1 (part0)
|
||||
// // comment out for orignal/solution0/solution2
|
||||
// // let mut vec1 = fill_vec(&vec0);
|
||||
|
||||
// // solution2 (part1)
|
||||
// // comment out for original/solution0/solution1
|
||||
// // fill_vec(vec0);
|
||||
|
||||
// // solution3 (part1)
|
||||
// // pass vec0_copy to fill_vec
|
||||
// let mut vec1 = fill_vec(vec0_copy);
|
||||
|
||||
// // original code
|
||||
// // Do not change the following line!
|
||||
// println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
|
||||
|
||||
// // solution2 (part2)
|
||||
// // comment out for original/solution0/solution1
|
||||
// // let mut vec1 = &mut Vec::new();
|
||||
// // fill_vec(vec1);
|
||||
|
||||
// // original code
|
||||
// // needed for original and all solutions
|
||||
// vec1.push(88);
|
||||
|
||||
// // original code
|
||||
// // needed for original and all solutions
|
||||
// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
// }
|
||||
|
||||
// // original code
|
||||
// // needed for original and solution0
|
||||
// // comment out for all solution1/solution2
|
||||
// fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||
// let mut vec = vec;
|
||||
|
||||
// vec.push(22);
|
||||
// vec.push(44);
|
||||
// vec.push(66);
|
||||
|
||||
// vec
|
||||
// }
|
||||
|
||||
// solution1 (part1)
|
||||
// comment out for original/solution0/solution2
|
||||
// fn fill_vec(vec: &Vec<i32>) -> Vec<i32> {
|
||||
// let mut vec = vec.to_vec();
|
||||
|
||||
// vec.push(22);
|
||||
// vec.push(44);
|
||||
// vec.push(66);
|
||||
|
||||
// vec
|
||||
// }
|
||||
|
||||
// solution2 (part0)
|
||||
// comment out for original/solution0/solution1
|
||||
// fn fill_vec(vec: &mut Vec<i32>) {
|
||||
// let mut vec = vec;
|
||||
|
||||
// vec.push(22);
|
||||
// vec.push(44);
|
||||
// vec.push(66);
|
||||
// }
|
||||
|
||||
// // original code
|
||||
// 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);
|
||||
|
||||
// // Do not change the following line!
|
||||
// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
// }
|
||||
|
||||
// // original code (cont'd)
|
||||
// fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||
// let mut vec = vec;
|
||||
|
||||
// vec.push(22);
|
||||
// vec.push(44);
|
||||
// vec.push(66);
|
||||
|
||||
// vec
|
||||
// }
|
||||
// // end of original code
|
||||
|
||||
// solution0
|
||||
// fn main() {
|
||||
// let vec0 = Vec::new();
|
||||
|
||||
// // create copy of vec0
|
||||
// let vec0_copy = vec0.clone();
|
||||
|
||||
// // pass vec0_copy to fill_vec
|
||||
// let mut vec1 = fill_vec(vec0_copy);
|
||||
|
||||
// // 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);
|
||||
// }
|
||||
|
||||
// // solution0 (cont'd)
|
||||
// fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||
// let mut vec = vec;
|
||||
|
||||
// vec.push(22);
|
||||
// vec.push(44);
|
||||
// vec.push(66);
|
||||
|
||||
// vec
|
||||
// }
|
||||
// // end of solution0
|
||||
|
||||
// // solution1
|
||||
// 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);
|
||||
|
||||
// // Do not change the following line!
|
||||
// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
// }
|
||||
|
||||
// // solution1 (cont'd)
|
||||
// fn fill_vec(vec: &Vec<i32>) -> Vec<i32> {
|
||||
// // convert borrowed vec to mutable copy
|
||||
// let mut vec = vec.to_vec().clone();
|
||||
|
||||
// vec.push(22);
|
||||
// vec.push(44);
|
||||
// vec.push(66);
|
||||
|
||||
// vec
|
||||
// }
|
||||
// // end of solution1
|
||||
|
||||
// solution2
|
||||
fn main() {
|
||||
let vec0 = Vec::new();
|
||||
// create mutable ref to vec
|
||||
let mut vec0 = &mut Vec::new();
|
||||
|
||||
// solution 1
|
||||
// let mut vec0_copy = &vec0;
|
||||
// let mut vec1 = fill_vec(vec0_copy.to_vec());
|
||||
let mut vec1 = fill_vec(vec0);
|
||||
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;
|
||||
|
||||
// solution2 (cont'd)
|
||||
// accept mutable ref to vec as argument and act on it directly
|
||||
fn fill_vec(vec: &mut Vec<i32>) {
|
||||
vec.push(22);
|
||||
vec.push(44);
|
||||
vec.push(66);
|
||||
|
||||
vec
|
||||
}
|
||||
// end of solution2
|
||||
|
||||
67
exercises/move_semantics/move_semantics2.rs.bak
Normal file
67
exercises/move_semantics/move_semantics2.rs.bak
Normal file
@ -0,0 +1,67 @@
|
||||
// move_semantics2.rs
|
||||
// Make me compile without changing line 13 or moving line 10!
|
||||
// Execute `rustlings hint move_semantics2` for hints :)
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn main() {
|
||||
// original code
|
||||
// comment out for solution1/solution2
|
||||
let vec0 = Vec::new();
|
||||
|
||||
// solution1 (part0)
|
||||
// let mut vec0 = Vec::new();
|
||||
|
||||
// solution2 (part0)
|
||||
// let vec0 = &mut Vec::new();
|
||||
|
||||
// solution0
|
||||
let mut vec0_copy = &vec0;
|
||||
let mut vec1 = fill_vec(vec0_copy.to_vec());
|
||||
|
||||
// original code
|
||||
let mut vec1 = fill_vec(vec0);
|
||||
|
||||
// solution1 (part0)
|
||||
// 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);
|
||||
}
|
||||
|
||||
// original code
|
||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||
let mut vec = vec;
|
||||
|
||||
vec.push(22);
|
||||
vec.push(44);
|
||||
vec.push(66);
|
||||
|
||||
vec
|
||||
}
|
||||
|
||||
// solution1 (part1)
|
||||
// fn fill_vec(vec: &Vec<i32>) -> Vec<i32> {
|
||||
// let mut vec = vec.to_vec();
|
||||
|
||||
// vec.push(22);
|
||||
// vec.push(44);
|
||||
// vec.push(66);
|
||||
|
||||
// vec
|
||||
// }
|
||||
|
||||
// solution2
|
||||
// fn fill_vec(vec: &mut Vec<i32>) -> Vec<i32> {
|
||||
// let mut vec = vec.clone();
|
||||
|
||||
// vec.push(22);
|
||||
// vec.push(44);
|
||||
// vec.push(66);
|
||||
|
||||
// vec
|
||||
// }
|
||||
@ -3,8 +3,30 @@
|
||||
// (no lines with multiple semicolons necessary!)
|
||||
// Execute `rustlings hint move_semantics3` for hints :)
|
||||
|
||||
// I AM NOT DONE
|
||||
// // original code
|
||||
// fn main() {
|
||||
// let vec0 = Vec::new();
|
||||
|
||||
// let mut vec1 = fill_vec(vec0);
|
||||
|
||||
// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
|
||||
// vec1.push(88);
|
||||
|
||||
// println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
// }
|
||||
|
||||
// // original code (cont'd)
|
||||
// fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||
// vec.push(22);
|
||||
// vec.push(44);
|
||||
// vec.push(66);
|
||||
|
||||
// vec
|
||||
// }
|
||||
// // end of original code
|
||||
|
||||
// solution
|
||||
fn main() {
|
||||
let vec0 = Vec::new();
|
||||
|
||||
@ -17,10 +39,13 @@ fn main() {
|
||||
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
|
||||
}
|
||||
|
||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||
// solution (cont'd)
|
||||
// change vec argument to mutable
|
||||
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
|
||||
vec.push(22);
|
||||
vec.push(44);
|
||||
vec.push(66);
|
||||
|
||||
vec
|
||||
}
|
||||
// end of solution
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user