Finish till move semantics

This commit is contained in:
Sangeeth Sudheer 2024-01-20 12:17:05 +05:30
parent 91ab971a04
commit 7e226a7433
Signed by: x
GPG Key ID: F6D06ECE734C57D1
8 changed files with 16 additions and 34 deletions

View File

@ -3,19 +3,19 @@
// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand // Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE
#[test] #[test]
fn main() { fn main() {
let vec0 = vec![22, 44, 66]; let vec0 = vec![22, 44, 66];
let vec1 = fill_vec(vec0); let vec1 = fill_vec(vec0);
// dbg!(vec0);
assert_eq!(vec1, vec![22, 44, 66, 88]); assert_eq!(vec1, vec![22, 44, 66, 88]);
} }
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let vec = vec; let mut vec = vec;
vec.push(88); vec.push(88);

View File

@ -5,13 +5,11 @@
// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand // Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE
#[test] #[test]
fn main() { fn main() {
let vec0 = vec![22, 44, 66]; let vec0 = vec![22, 44, 66];
let mut vec1 = fill_vec(vec0); let mut vec1 = fill_vec(vec0.clone());
assert_eq!(vec0, vec![22, 44, 66]); assert_eq!(vec0, vec![22, 44, 66]);
assert_eq!(vec1, vec![22, 44, 66, 88]); assert_eq!(vec1, vec![22, 44, 66, 88]);

View File

@ -6,8 +6,6 @@
// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand // Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE
#[test] #[test]
fn main() { fn main() {
let vec0 = vec![22, 44, 66]; let vec0 = vec![22, 44, 66];
@ -17,7 +15,7 @@ fn main() {
assert_eq!(vec1, vec![22, 44, 66, 88]); assert_eq!(vec1, vec![22, 44, 66, 88]);
} }
fn fill_vec(vec: Vec<i32>) -> Vec<i32> { fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(88); vec.push(88);
vec vec

View File

@ -7,13 +7,11 @@
// Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand // Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE
#[test] #[test]
fn main() { fn main() {
let vec0 = vec![22, 44, 66]; let vec0 = vec![22, 44, 66];
let mut vec1 = fill_vec(vec0); let mut vec1 = fill_vec();
assert_eq!(vec1, vec![22, 44, 66, 88]); assert_eq!(vec1, vec![22, 44, 66, 88]);
} }
@ -21,9 +19,5 @@ fn main() {
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this! // `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this!
fn fill_vec() -> Vec<i32> { fn fill_vec() -> Vec<i32> {
// Instead, let's create and fill the Vec in here - how do you do that? // Instead, let's create and fill the Vec in here - how do you do that?
let mut vec = vec; vec![22, 44, 66, 88]
vec.push(88);
vec
} }

View File

@ -6,14 +6,12 @@
// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand // Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE
#[test] #[test]
fn main() { fn main() {
let mut x = 100; let mut x = 100;
let y = &mut x; let y = &mut x;
let z = &mut x;
*y += 100; *y += 100;
let z = &mut x;
*z += 1000; *z += 1000;
assert_eq!(x, 1200); assert_eq!(x, 1200);
} }

View File

@ -5,24 +5,22 @@
// Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand // Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE
fn main() { fn main() {
let data = "Rust is great!".to_string(); let data = "Rust is great!".to_string();
get_char(data); get_char(&data);
string_uppercase(&data); string_uppercase(data);
} }
// Should not take ownership // Should not take ownership
fn get_char(data: String) -> char { fn get_char(data: &String) -> char {
data.chars().last().unwrap() data.chars().last().unwrap()
} }
// Should take ownership // Should take ownership
fn string_uppercase(mut data: &String) { fn string_uppercase(mut data: String) {
data = &data.to_uppercase(); data = data.to_uppercase();
println!("{}", data); println!("{}", data);
} }

View File

@ -7,11 +7,9 @@
// //
// Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn array_and_vec() -> ([i32; 4], Vec<i32>) { fn array_and_vec() -> ([i32; 4], Vec<i32>) {
let a = [10, 20, 30, 40]; // a plain array let a = [10, 20, 30, 40]; // a plain array
let v = // TODO: declare your vector here with the macro for vectors let v = vec![10, 20, 30, 40]; // TODO: declare your vector here with the macro for vectors
(a, v) (a, v)
} }

View File

@ -7,13 +7,11 @@
// //
// Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> { fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for element in v.iter_mut() { for element in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is // TODO: Fill this up so that each element in the Vec `v` is
// multiplied by 2. // multiplied by 2.
??? *element *= 2;
} }
// At this point, `v` should be equal to [4, 8, 12, 16, 20]. // At this point, `v` should be equal to [4, 8, 12, 16, 20].
@ -24,7 +22,7 @@ fn vec_map(v: &Vec<i32>) -> Vec<i32> {
v.iter().map(|element| { v.iter().map(|element| {
// TODO: Do the same thing as above - but instead of mutating the // TODO: Do the same thing as above - but instead of mutating the
// Vec, you can just return the new number! // Vec, you can just return the new number!
??? element * 2
}).collect() }).collect()
} }