diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index e2f5876d..7ac2670a 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -1,12 +1,11 @@ // move_semantics1.rs // Make me compile! Execute `rustlings hint move_semantics1` for hints :) -// I AM NOT DONE fn main() { let vec0 = Vec::new(); - let vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(vec0); // We need mut because we're modifying vec1 in line 13 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 bd21fbb7..cc690e74 100644 --- a/exercises/move_semantics/move_semantics2.rs +++ b/exercises/move_semantics/move_semantics2.rs @@ -1,13 +1,10 @@ // move_semantics2.rs // Make me compile without changing line 13! // Execute `rustlings hint move_semantics2` for hints :) - -// I AM NOT DONE - fn main() { - let vec0 = Vec::new(); + let mut vec0 = Vec::new(); - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(&mut vec0); // We need to set line 5 to mutable, so we can mutably borrow vec0, this allows us to use vec.push in our function below // Do not change the following line! println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); @@ -17,12 +14,13 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -fn fill_vec(vec: Vec) -> Vec { - let mut vec = vec; +fn fill_vec(vec: &mut Vec) -> Vec { + //here we need &mut because our input vectors are mutable + let vec = vec; vec.push(22); vec.push(44); vec.push(66); - vec -} + vec.to_vec() // we need to transform vec into an immutable (type Vec not &Vec, see function defintion if you're still confused) +} \ No newline at end of file diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs index 43fef74f..b5ba1793 100644 --- a/exercises/move_semantics/move_semantics3.rs +++ b/exercises/move_semantics/move_semantics3.rs @@ -3,12 +3,12 @@ // (no lines with multiple semicolons necessary!) // Execute `rustlings hint move_semantics3` for hints :) -// I AM NOT DONE +// Not entirely sure how this differs from move_semantics2.rs ... fn main() { - let vec0 = Vec::new(); + let mut vec0 = Vec::new(); - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(&mut vec0); println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); @@ -17,10 +17,10 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -fn fill_vec(vec: Vec) -> Vec { +fn fill_vec(vec: &mut Vec) -> Vec { vec.push(22); vec.push(44); vec.push(66); - vec + vec.to_vec() } diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs index 2a23c710..eb18a666 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -4,12 +4,11 @@ // freshly created vector from fill_vec to its caller. // Execute `rustlings hint move_semantics4` for hints! -// I AM NOT DONE +// Hopefully what I did is clear for posterity. 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 +19,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 c4704f9e..c3b0b60b 100644 --- a/exercises/move_semantics/move_semantics5.rs +++ b/exercises/move_semantics/move_semantics5.rs @@ -3,13 +3,12 @@ // adding, changing or removing any of them. // Execute `rustlings hint move_semantics5` for hints :) -// I AM NOT DONE fn main() { let mut x = 100; let y = &mut x; + *y += 100; // This works because otherwise we'd be borrowing something twice, without doing anything with it! let z = &mut x; - *y += 100; *z += 1000; assert_eq!(x, 1200); } diff --git a/exercises/primitive_types/primitive_types1.rs b/exercises/primitive_types/primitive_types1.rs index 09121392..2466419e 100644 --- a/exercises/primitive_types/primitive_types1.rs +++ b/exercises/primitive_types/primitive_types1.rs @@ -2,7 +2,6 @@ // Fill in the rest of the line that has code missing! // No hints, there's no tricks, just get used to typing these :) -// I AM NOT DONE fn main() { // Booleans (`bool`) @@ -12,7 +11,7 @@ fn main() { println!("Good morning!"); } - let // Finish the rest of this line like the example! Or make it be false! + let is_evening = false;// Finish the rest of this line like the example! Or make it be false! if is_evening { println!("Good evening!"); } diff --git a/exercises/primitive_types/primitive_types2.rs b/exercises/primitive_types/primitive_types2.rs index 6576a4d5..656ff612 100644 --- a/exercises/primitive_types/primitive_types2.rs +++ b/exercises/primitive_types/primitive_types2.rs @@ -2,7 +2,6 @@ // Fill in the rest of the line that has code missing! // No hints, there's no tricks, just get used to typing these :) -// I AM NOT DONE fn main() { // Characters (`char`) @@ -16,7 +15,7 @@ fn main() { println!("Neither alphabetic nor numeric!"); } - let // Finish this line like the example! What's your favorite character? + let your_character = '3'; // Finish this line like the example! What's your favorite character? // Try a letter, try a number, try a special character, try a character // from a different language than your own, try an emoji! if your_character.is_alphabetic() { diff --git a/exercises/primitive_types/primitive_types3.rs b/exercises/primitive_types/primitive_types3.rs index aaa518be..a5ac9d68 100644 --- a/exercises/primitive_types/primitive_types3.rs +++ b/exercises/primitive_types/primitive_types3.rs @@ -2,14 +2,13 @@ // Create an array with at least 100 elements in it where the ??? is. // Execute `rustlings hint primitive_types3` for hints! -// I AM NOT DONE fn main() { - let a = ??? + let a = [1,2,3]; // This is the "array" type in Rust if a.len() >= 100 { println!("Wow, that's a big array!"); } else { println!("Meh, I eat arrays like that for breakfast."); } -} +} \ No newline at end of file diff --git a/exercises/primitive_types/primitive_types4.rs b/exercises/primitive_types/primitive_types4.rs index 10b553e9..2debd18a 100644 --- a/exercises/primitive_types/primitive_types4.rs +++ b/exercises/primitive_types/primitive_types4.rs @@ -2,13 +2,12 @@ // Get a slice out of Array a where the ??? is so that the test passes. // Execute `rustlings hint primitive_types4` for hints!! -// I AM NOT DONE #[test] fn slice_out_of_array() { let a = [1, 2, 3, 4, 5]; - let nice_slice = ??? + let nice_slice = &a[1..4]; // this is how you slice arrays, much like slicing python lists, but you have to borrow the variable first, and it goes &array[i..n] and you get values from i to n-1. assert_eq!([2, 3, 4], nice_slice) } diff --git a/exercises/primitive_types/primitive_types5.rs b/exercises/primitive_types/primitive_types5.rs index 680d8d23..c87ce4ea 100644 --- a/exercises/primitive_types/primitive_types5.rs +++ b/exercises/primitive_types/primitive_types5.rs @@ -2,11 +2,10 @@ // Destructure the `cat` tuple so that the println will work. // Execute `rustlings hint primitive_types5` for hints! -// I AM NOT DONE fn main() { let cat = ("Furry McFurson", 3.5); - let /* your pattern here */ = cat; + let (name,age) = cat; // This is like python, but you need parentheses println!("{} is {} years old.", name, age); } diff --git a/exercises/primitive_types/primitive_types6.rs b/exercises/primitive_types/primitive_types6.rs index b8c9b82b..bbbbcee5 100644 --- a/exercises/primitive_types/primitive_types6.rs +++ b/exercises/primitive_types/primitive_types6.rs @@ -3,13 +3,12 @@ // You can put the expression for the second element where ??? is so that the test passes. // Execute `rustlings hint primitive_types6` for hints! -// I AM NOT DONE #[test] fn indexing_tuple() { let numbers = (1, 2, 3); // Replace below ??? with the tuple indexing syntax. - let second = ???; + let second = numbers.1; // This is pretty interesting, and a great example of where Rust differs strongly from python. I had guessed the answer was &numbers[1], but the compiler corrected me! assert_eq!(2, second, "This is not the 2nd number in the tuple!") diff --git a/exercises/structs/structs1.rs b/exercises/structs/structs1.rs index 6d0b2f49..e7818186 100644 --- a/exercises/structs/structs1.rs +++ b/exercises/structs/structs1.rs @@ -1,17 +1,17 @@ // structs1.rs // Address all the TODOs to make the tests pass! -// I AM NOT DONE struct ColorClassicStruct { // TODO: Something goes here + name: String, + hex: String, } -struct ColorTupleStruct(/* TODO: Something goes here */); +struct ColorTupleStruct(String, String); #[derive(Debug)] struct UnitStruct; - #[cfg(test)] mod tests { use super::*; @@ -19,7 +19,10 @@ mod tests { #[test] fn classic_c_structs() { // TODO: Instantiate a classic c struct! - // let green = + let green = ColorClassicStruct { + name: String::from("green"), + hex: String::from("#00FF00"), + }; assert_eq!(green.name, "green"); assert_eq!(green.hex, "#00FF00"); @@ -28,7 +31,7 @@ mod tests { #[test] fn tuple_structs() { // TODO: Instantiate a tuple struct! - // let green = + let green = ColorTupleStruct(String::from("green"),String::from("#00FF00")); assert_eq!(green.0, "green"); assert_eq!(green.1, "#00FF00"); @@ -37,7 +40,7 @@ mod tests { #[test] fn unit_structs() { // TODO: Instantiate a unit struct! - // let unit_struct = + let unit_struct = UnitStruct ; // Woah, so this is pulling UnitStruct as a string, and if I add parameters, you'd see them here, too! let message = format!("{:?}s are fun!", unit_struct); assert_eq!(message, "UnitStructs are fun!"); diff --git a/exercises/structs/structs2.rs b/exercises/structs/structs2.rs index f9c6427d..b1330b78 100644 --- a/exercises/structs/structs2.rs +++ b/exercises/structs/structs2.rs @@ -1,7 +1,6 @@ // structs2.rs // Address all the TODOs to make the tests pass! -// I AM NOT DONE #[derive(Debug)] struct Order { @@ -34,7 +33,9 @@ mod tests { 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 mut your_order = create_order_template(); + your_order.name = String::from("Hacker in Rust"); + your_order.count = 1; 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/structs/structs3.rs b/exercises/structs/structs3.rs index e84f2ebc..27436063 100644 --- a/exercises/structs/structs3.rs +++ b/exercises/structs/structs3.rs @@ -4,7 +4,6 @@ // Make the code compile and the tests pass! // If you have issues execute `rustlings hint structs3` -// I AM NOT DONE #[derive(Debug)] struct Package { @@ -16,7 +15,7 @@ struct Package { impl Package { fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package { if weight_in_grams <= 0 { - // panic statement goes here... + panic!("The weight {} is not possible!",weight_in_grams) } else { Package { sender_country, @@ -26,12 +25,12 @@ impl Package { } } - fn is_international(&self) -> ??? { - // Something goes here... + fn is_international(&self) -> bool { + self.recipient_country != self.sender_country } - fn get_fees(&self, cents_per_gram: i32) -> ??? { - // Something goes here... + fn get_fees(&self, cents_per_gram: i32) -> i32 { + self.weight_in_grams*cents_per_gram } }