From 99745f1acfed087306f2ae6f9fadc86084b0c6db Mon Sep 17 00:00:00 2001 From: perro Date: Fri, 17 Feb 2023 10:44:18 -0800 Subject: [PATCH] Ejercicios 32/94 --- exercises/move_semantics/move_semantics1.rs | 4 +--- exercises/move_semantics/move_semantics2.rs | 4 +--- exercises/move_semantics/move_semantics3.rs | 4 +--- exercises/move_semantics/move_semantics4.rs | 8 ++------ exercises/move_semantics/move_semantics5.rs | 4 +--- exercises/move_semantics/move_semantics6.rs | 12 +++++------- exercises/structs/structs1.rs | 21 +++++++++++---------- exercises/structs/structs2.rs | 9 +++++---- exercises/vecs/vecs1.rs | 5 ++--- exercises/vecs/vecs2.rs | 10 ++-------- 10 files changed, 31 insertions(+), 50 deletions(-) diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index aac6dfc..c60ee74 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -1,12 +1,10 @@ // move_semantics1.rs // Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let vec0 = Vec::new(); - let vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(vec0); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); diff --git a/exercises/move_semantics/move_semantics2.rs b/exercises/move_semantics/move_semantics2.rs index 6487085..8825a87 100644 --- a/exercises/move_semantics/move_semantics2.rs +++ b/exercises/move_semantics/move_semantics2.rs @@ -2,12 +2,10 @@ // Make me compile without changing line 13 or moving line 10! // Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let vec0 = Vec::new(); - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(vec0.clone()); // Do not change the following line! println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs index eaa30e3..bb83023 100644 --- a/exercises/move_semantics/move_semantics3.rs +++ b/exercises/move_semantics/move_semantics3.rs @@ -3,8 +3,6 @@ // (no lines with multiple semicolons necessary!) // Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let vec0 = Vec::new(); @@ -17,7 +15,7 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -fn fill_vec(vec: Vec) -> Vec { +fn fill_vec(mut vec: Vec) -> Vec { vec.push(22); vec.push(44); vec.push(66); diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs index 99834ec..2107761 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -4,12 +4,8 @@ // function. // Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { - let vec0 = Vec::new(); - - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); @@ -20,7 +16,7 @@ fn main() { // `fill_vec()` no longer takes `vec: Vec` as argument fn fill_vec() -> Vec { - let mut vec = vec; + let mut vec = Vec::new(); vec.push(22); vec.push(44); diff --git a/exercises/move_semantics/move_semantics5.rs b/exercises/move_semantics/move_semantics5.rs index 36eae12..ab04f4c 100644 --- a/exercises/move_semantics/move_semantics5.rs +++ b/exercises/move_semantics/move_semantics5.rs @@ -3,13 +3,11 @@ // adding, changing or removing any of them. // Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let mut x = 100; let y = &mut x; - let z = &mut x; *y += 100; + let z = &mut x; *z += 1000; assert_eq!(x, 1200); } diff --git a/exercises/move_semantics/move_semantics6.rs b/exercises/move_semantics/move_semantics6.rs index eb52a84..055ea5b 100644 --- a/exercises/move_semantics/move_semantics6.rs +++ b/exercises/move_semantics/move_semantics6.rs @@ -2,24 +2,22 @@ // Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand for a hint. // You can't change anything except adding or removing references. -// I AM NOT DONE - fn main() { let data = "Rust is great!".to_string(); - get_char(data); + get_char(&data); - string_uppercase(&data); + string_uppercase(data); } // Should not take ownership -fn get_char(data: String) -> char { +fn get_char(data: &String) -> char { data.chars().last().unwrap() } // Should take ownership -fn string_uppercase(mut data: &String) { - data = &data.to_uppercase(); +fn string_uppercase(mut data: String) { + data = data.to_uppercase(); println!("{}", data); } diff --git a/exercises/structs/structs1.rs b/exercises/structs/structs1.rs index 0d91c46..3debc74 100644 --- a/exercises/structs/structs1.rs +++ b/exercises/structs/structs1.rs @@ -2,13 +2,13 @@ // Address all the TODOs to make the tests pass! // Execute `rustlings hint structs1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - struct ColorClassicStruct { - // TODO: Something goes here + red: u32, + green: u32, + blue: u32, } -struct ColorTupleStruct(/* TODO: Something goes here */); +struct ColorTupleStruct(u32, u32, u32); #[derive(Debug)] struct UnitLikeStruct; @@ -19,8 +19,11 @@ mod tests { #[test] fn classic_c_structs() { - // TODO: Instantiate a classic c struct! - // let green = + let green = ColorClassicStruct { + red: 0, + green: 255, + blue: 0, + }; assert_eq!(green.red, 0); assert_eq!(green.green, 255); @@ -29,8 +32,7 @@ mod tests { #[test] fn tuple_structs() { - // TODO: Instantiate a tuple struct! - // let green = + let green = ColorTupleStruct(0, 255, 0); assert_eq!(green.0, 0); assert_eq!(green.1, 255); @@ -39,8 +41,7 @@ mod tests { #[test] fn unit_structs() { - // TODO: Instantiate a unit-like struct! - // let unit_like_struct = + let unit_like_struct = UnitLikeStruct; let message = format!("{:?}s are fun!", unit_like_struct); assert_eq!(message, "UnitLikeStructs are fun!"); diff --git a/exercises/structs/structs2.rs b/exercises/structs/structs2.rs index 32e311f..b9ad697 100644 --- a/exercises/structs/structs2.rs +++ b/exercises/structs/structs2.rs @@ -2,8 +2,6 @@ // Address all the TODOs to make the tests pass! // Execute `rustlings hint structs2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[derive(Debug)] struct Order { name: String, @@ -34,8 +32,11 @@ mod tests { #[test] fn your_order() { let order_template = create_order_template(); - // TODO: Create your own order using the update syntax and template above! - // let your_order = + let your_order = Order { + name: String::from("Hacker in Rust"), + count: 1, + ..order_template + }; assert_eq!(your_order.name, "Hacker in Rust"); assert_eq!(your_order.year, order_template.year); assert_eq!(your_order.made_by_phone, order_template.made_by_phone); diff --git a/exercises/vecs/vecs1.rs b/exercises/vecs/vecs1.rs index 4e8c4cb..36d1b4c 100644 --- a/exercises/vecs/vecs1.rs +++ b/exercises/vecs/vecs1.rs @@ -4,11 +4,10 @@ // Make me compile and pass the test! // Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn array_and_vec() -> ([i32; 4], Vec) { let a = [10, 20, 30, 40]; // a plain array - let v = // TODO: declare your vector here with the macro for vectors + let v = a.to_vec(); + // let v = vec![10, 20, 30, 40]; (a, v) } diff --git a/exercises/vecs/vecs2.rs b/exercises/vecs/vecs2.rs index 5bea09a..5d04bd4 100644 --- a/exercises/vecs/vecs2.rs +++ b/exercises/vecs/vecs2.rs @@ -6,13 +6,9 @@ // // Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn vec_loop(mut v: Vec) -> Vec { for i in v.iter_mut() { - // TODO: Fill this up so that each element in the Vec `v` is - // multiplied by 2. - ??? + *i *= 2 } // At this point, `v` should be equal to [4, 8, 12, 16, 20]. @@ -21,9 +17,7 @@ fn vec_loop(mut v: Vec) -> Vec { fn vec_map(v: &Vec) -> Vec { v.iter().map(|num| { - // TODO: Do the same thing as above - but instead of mutating the - // Vec, you can just return the new number! - ??? + num * 2 }).collect() }