diff --git a/exercises/enums/enums1.rs b/exercises/enums/enums1.rs index 511ba740e2..535d21e1ce 100644 --- a/exercises/enums/enums1.rs +++ b/exercises/enums/enums1.rs @@ -1,11 +1,13 @@ // enums1.rs // No hints this time! ;) -// I AM NOT DONE #[derive(Debug)] enum Message { - // TODO: define a few types of messages as used below + Quit, + Echo, + Move, + ChangeColor } fn main() { diff --git a/exercises/enums/enums2.rs b/exercises/enums/enums2.rs index 18479f874d..5a58a2f108 100644 --- a/exercises/enums/enums2.rs +++ b/exercises/enums/enums2.rs @@ -1,11 +1,13 @@ // enums2.rs // Execute `rustlings hint enums2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE #[derive(Debug)] enum Message { - // TODO: define the different variants used below + Move {x: i32, y: i32}, + Echo(String), + ChangeColor(i32, i32, i32), + Quit } impl Message { diff --git a/exercises/enums/enums3.rs b/exercises/enums/enums3.rs index 55acf6bc9d..54b9d225ce 100644 --- a/exercises/enums/enums3.rs +++ b/exercises/enums/enums3.rs @@ -2,10 +2,12 @@ // Address all the TODOs to make the tests pass! // Execute `rustlings hint enums3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE enum Message { - // TODO: implement the message variant types based on their usage below + ChangeColor((u8, u8, u8)), + Echo(String), + Move(Point), + Quit } struct Point { @@ -37,7 +39,12 @@ impl State { } fn process(&mut self, message: Message) { - // TODO: create a match expression to process the different message variants + match message { + Message::ChangeColor(color) => self.change_color(color), + Message::Echo(string) => self.echo(string), + Message::Move(position) => self.move_position(position), + Message::Quit => self.quit() + } } } diff --git a/exercises/error_handling/errors1.rs b/exercises/error_handling/errors1.rs index bcee9723e9..6aaef210ab 100644 --- a/exercises/error_handling/errors1.rs +++ b/exercises/error_handling/errors1.rs @@ -5,14 +5,13 @@ // construct to `Option` that can be used to express error conditions. Let's use it! // Execute `rustlings hint errors1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE -pub fn generate_nametag_text(name: String) -> Option { + +pub fn generate_nametag_text(name: String) -> Result { if name.is_empty() { - // Empty names aren't allowed. - None + Err("`name` was empty; it must be nonempty.".into()) } else { - Some(format!("Hi! My name is {}", name)) + Ok(format!("Hi! My name is {}", name)) } } diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs index 1cd8fc66b0..35e090c59d 100644 --- a/exercises/error_handling/errors2.rs +++ b/exercises/error_handling/errors2.rs @@ -17,16 +17,17 @@ // one is a lot shorter! // Execute `rustlings hint errors2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::num::ParseIntError; pub fn total_cost(item_quantity: &str) -> Result { let processing_fee = 1; let cost_per_item = 5; let qty = item_quantity.parse::(); - - Ok(qty * cost_per_item + processing_fee) + match qty + { + Ok(value) => Ok(value * cost_per_item + processing_fee), _ => qty + } + } #[cfg(test)] diff --git a/exercises/error_handling/errors3.rs b/exercises/error_handling/errors3.rs index a2d2d1901d..c9784e3d54 100644 --- a/exercises/error_handling/errors3.rs +++ b/exercises/error_handling/errors3.rs @@ -4,11 +4,9 @@ // Why not? What should we do to fix it? // Execute `rustlings hint errors3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::num::ParseIntError; -fn main() { +fn main() -> Result<(), ParseIntError> { let mut tokens = 100; let pretend_user_input = "8"; @@ -20,6 +18,7 @@ fn main() { tokens -= cost; println!("You now have {} tokens.", tokens); } + Ok(()) } pub fn total_cost(item_quantity: &str) -> Result { diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs index 0efe8ccd5a..fade9f4f2f 100644 --- a/exercises/error_handling/errors4.rs +++ b/exercises/error_handling/errors4.rs @@ -1,7 +1,6 @@ // errors4.rs // Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); @@ -14,7 +13,11 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result { - // Hmm...? Why is this only returning an Ok value? + if value < 0 { + return Err(CreationError::Negative) + } else if value == 0 { + return Err(CreationError::Zero) + } Ok(PositiveNonzeroInteger(value as u64)) } } diff --git a/exercises/error_handling/errors5.rs b/exercises/error_handling/errors5.rs index 2ba8f9039d..f31e13f51e 100644 --- a/exercises/error_handling/errors5.rs +++ b/exercises/error_handling/errors5.rs @@ -23,7 +23,7 @@ use std::fmt; use std::num::ParseIntError; // TODO: update the return type of `main()` to make this compile. -fn main() -> Result<(), Box> { +fn main() -> Result<(), Box> { let pretend_user_input = "42"; let x: i64 = pretend_user_input.parse()?; println!("output={:?}", PositiveNonzeroInteger::new(x)?); diff --git a/exercises/error_handling/errors6.rs b/exercises/error_handling/errors6.rs index 1306fb03e1..7ee3d2deba 100644 --- a/exercises/error_handling/errors6.rs +++ b/exercises/error_handling/errors6.rs @@ -23,16 +23,16 @@ impl ParsePosNonzeroError { fn from_creation(err: CreationError) -> ParsePosNonzeroError { ParsePosNonzeroError::Creation(err) } - // TODO: add another error conversion function here. - // fn from_parseint... + + fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError { + ParsePosNonzeroError::ParseInt(err) + } } fn parse_pos_nonzero(s: &str) -> Result { - // TODO: change this to return an appropriate error instead of panicking - // when `parse()` returns an error. - let x: i64 = s.parse().unwrap(); + let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?; PositiveNonzeroInteger::new(x) .map_err(ParsePosNonzeroError::from_creation) } diff --git a/exercises/intro/intro2.rs b/exercises/intro/intro2.rs index efc1af2059..7ba39ca86c 100644 --- a/exercises/intro/intro2.rs +++ b/exercises/intro/intro2.rs @@ -2,8 +2,7 @@ // Make the code print a greeting to the world. // Execute `rustlings hint intro2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { - println!("Hello {}!"); + let greeting = "World"; + println!("Hello {}!", greeting); } diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs index aac6dfc39c..a636de7d24 100644 --- a/exercises/move_semantics/move_semantics1.rs +++ b/exercises/move_semantics/move_semantics1.rs @@ -6,7 +6,7 @@ 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 64870850ec..db66c01adf 100644 --- a/exercises/move_semantics/move_semantics2.rs +++ b/exercises/move_semantics/move_semantics2.rs @@ -7,7 +7,7 @@ fn main() { let vec0 = Vec::new(); - let mut vec1 = fill_vec(vec0); + let mut vec1 = fill_vec(&vec0); // Do not change the following line! println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); @@ -17,8 +17,8 @@ fn main() { println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); } -fn fill_vec(vec: Vec) -> Vec { - let mut vec = vec; +fn fill_vec(vec: &Vec) -> Vec { + let mut vec = vec.to_vec(); vec.push(22); vec.push(44); diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs index eaa30e3344..b5e4db2967 100644 --- a/exercises/move_semantics/move_semantics3.rs +++ b/exercises/move_semantics/move_semantics3.rs @@ -17,7 +17,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 99834ec34e..99130074ec 100644 --- a/exercises/move_semantics/move_semantics4.rs +++ b/exercises/move_semantics/move_semantics4.rs @@ -7,9 +7,8 @@ // 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 +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 36eae127ac..d25a01153e 100644 --- a/exercises/move_semantics/move_semantics5.rs +++ b/exercises/move_semantics/move_semantics5.rs @@ -8,8 +8,9 @@ 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 eb52a848d3..f8cef7f0d4 100644 --- a/exercises/move_semantics/move_semantics6.rs +++ b/exercises/move_semantics/move_semantics6.rs @@ -7,19 +7,19 @@ 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: &str) -> 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/standard_library_types/arc1.rs b/exercises/standard_library_types/arc1.rs index 93a27036f2..80cede2c9d 100644 --- a/exercises/standard_library_types/arc1.rs +++ b/exercises/standard_library_types/arc1.rs @@ -18,7 +18,6 @@ // where the second TODO comment is. Try not to create any copies of the `numbers` Vec! // Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE #![forbid(unused_imports)] // Do not change this, (or the next) line. use std::sync::Arc; @@ -26,11 +25,11 @@ use std::thread; fn main() { let numbers: Vec<_> = (0..100u32).collect(); - let shared_numbers = // TODO + let shared_numbers = Arc::new(numbers); let mut joinhandles = Vec::new(); for offset in 0..8 { - let child_numbers = // TODO + let child_numbers = shared_numbers.clone(); joinhandles.push(thread::spawn(move || { let sum: u32 = child_numbers.iter().filter(|n| *n % 8 == offset).sum(); println!("Sum of offset {} is {}", offset, sum); diff --git a/exercises/strings/strings1.rs b/exercises/strings/strings1.rs index 0de86a1dbf..d89d2b0e9c 100644 --- a/exercises/strings/strings1.rs +++ b/exercises/strings/strings1.rs @@ -2,13 +2,11 @@ // Make me compile without changing the function signature! // Execute `rustlings hint strings1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let answer = current_favorite_color(); println!("My current favorite color is {}", answer); } fn current_favorite_color() -> String { - "blue" + return "blue".to_string(); } diff --git a/exercises/strings/strings2.rs b/exercises/strings/strings2.rs index 0c48ec959d..26c2547663 100644 --- a/exercises/strings/strings2.rs +++ b/exercises/strings/strings2.rs @@ -2,8 +2,6 @@ // Make me compile without changing the function signature! // Execute `rustlings hint strings2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn main() { let word = String::from("green"); // Try not changing this line :) if is_a_color_word(word) { @@ -13,6 +11,6 @@ fn main() { } } -fn is_a_color_word(attempt: &str) -> bool { +fn is_a_color_word(attempt: String) -> bool { attempt == "green" || attempt == "blue" || attempt == "red" } diff --git a/exercises/strings/strings3.rs b/exercises/strings/strings3.rs index e2353aecc9..610e3d1938 100644 --- a/exercises/strings/strings3.rs +++ b/exercises/strings/strings3.rs @@ -1,21 +1,16 @@ // strings3.rs // Execute `rustlings hint strings3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - fn trim_me(input: &str) -> String { - // TODO: Remove whitespace from both ends of a string! - ??? + return input.trim().to_string(); } -fn compose_me(input: &str) -> String { - // TODO: Add " world!" to the string! There's multiple ways to do this! - ??? +fn compose_me(input: &str) -> String { + return input.to_string() + " world!"; } fn replace_me(input: &str) -> String { - // TODO: Replace "cars" in the string with "balloons"! - ??? + return input.replace("cars", "balloons"); } #[cfg(test)] diff --git a/exercises/strings/strings4.rs b/exercises/strings/strings4.rs index c410b56252..c5caaf8bdb 100644 --- a/exercises/strings/strings4.rs +++ b/exercises/strings/strings4.rs @@ -6,8 +6,6 @@ // before the parentheses on each line. If you're right, it will compile! // No hints this time! -// I AM NOT DONE - fn string_slice(arg: &str) { println!("{}", arg); } @@ -16,14 +14,14 @@ fn string(arg: String) { } fn main() { - ???("blue"); - ???("red".to_string()); - ???(String::from("hi")); - ???("rust is fun!".to_owned()); - ???("nice weather".into()); - ???(format!("Interpolation {}", "Station")); - ???(&String::from("abc")[0..1]); - ???(" hello there ".trim()); - ???("Happy Monday!".to_string().replace("Mon", "Tues")); - ???("mY sHiFt KeY iS sTiCkY".to_lowercase()); + string_slice("blue"); + string("red".to_string()); + string(String::from("hi")); + string("rust is fun!".to_owned()); + string_slice("nice weather".into()); + string(format!("Interpolation {}", "Station")); + string_slice(&String::from("abc")[0..1]); + string_slice(" hello there ".trim()); + string("Happy Monday!".to_string().replace("Mon", "Tues")); + string("mY sHiFt KeY iS sTiCkY".to_lowercase()); } diff --git a/exercises/structs/structs3.rs b/exercises/structs/structs3.rs index 3536a4573e..9ede201de2 100644 --- a/exercises/structs/structs3.rs +++ b/exercises/structs/structs3.rs @@ -26,12 +26,12 @@ impl Package { } } - fn is_international(&self) -> ??? { - // Something goes here... + fn is_international(&self) -> bool { + self.sender_country != self.recipient_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 } } diff --git a/exercises/tests/tests1.rs b/exercises/tests/tests1.rs index 8b6ea37469..3563e8e7fe 100644 --- a/exercises/tests/tests1.rs +++ b/exercises/tests/tests1.rs @@ -7,12 +7,12 @@ // pass! Make the test fail! // Execute `rustlings hint tests1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE #[cfg(test)] mod tests { #[test] fn you_can_assert() { - assert!(); + let x = 1; + assert!(x != 1); } } diff --git a/exercises/tests/tests2.rs b/exercises/tests/tests2.rs index a5ac15b11f..69e79ebbe0 100644 --- a/exercises/tests/tests2.rs +++ b/exercises/tests/tests2.rs @@ -3,12 +3,10 @@ // pass! Make the test fail! // Execute `rustlings hint tests2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[cfg(test)] mod tests { #[test] fn you_can_assert_eq() { - assert_eq!(); + assert_eq!(1, 2); } } diff --git a/exercises/tests/tests3.rs b/exercises/tests/tests3.rs index 196a81a0e5..28b0ebe036 100644 --- a/exercises/tests/tests3.rs +++ b/exercises/tests/tests3.rs @@ -4,8 +4,6 @@ // we expect to get when we call `is_even(5)`. // Execute `rustlings hint tests3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - pub fn is_even(num: i32) -> bool { num % 2 == 0 } @@ -16,11 +14,11 @@ mod tests { #[test] fn is_true_when_even() { - assert!(); + assert!(is_even(6)); } #[test] fn is_false_when_odd() { - assert!(); + assert!(!is_even(5)); } } diff --git a/exercises/variables/variables1.rs b/exercises/variables/variables1.rs index f4d182accf..b0ad72daf7 100644 --- a/exercises/variables/variables1.rs +++ b/exercises/variables/variables1.rs @@ -2,9 +2,8 @@ // Make me compile! // Execute `rustlings hint variables1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE fn main() { - x = 5; + let x = 5; println!("x has the value {}", x); } diff --git a/exercises/variables/variables2.rs b/exercises/variables/variables2.rs index 641aeb8e09..c4a5832d5f 100644 --- a/exercises/variables/variables2.rs +++ b/exercises/variables/variables2.rs @@ -1,10 +1,9 @@ // variables2.rs // Execute `rustlings hint variables2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE fn main() { - let x; + let x = 10; if x == 10 { println!("x is ten!"); } else { diff --git a/exercises/variables/variables3.rs b/exercises/variables/variables3.rs index 819b1bc791..b525f39715 100644 --- a/exercises/variables/variables3.rs +++ b/exercises/variables/variables3.rs @@ -4,6 +4,6 @@ // I AM NOT DONE fn main() { - let x: i32; + let x = 1; println!("Number {}", x); } diff --git a/exercises/variables/variables4.rs b/exercises/variables/variables4.rs index 54491b0a20..ae3d20e9fd 100644 --- a/exercises/variables/variables4.rs +++ b/exercises/variables/variables4.rs @@ -4,7 +4,7 @@ // I AM NOT DONE fn main() { - let x = 3; + let mut x = 3; println!("Number {}", x); x = 5; // don't change this line println!("Number {}", x); diff --git a/exercises/variables/variables5.rs b/exercises/variables/variables5.rs index 0e670d2afe..65faf126f8 100644 --- a/exercises/variables/variables5.rs +++ b/exercises/variables/variables5.rs @@ -6,6 +6,6 @@ fn main() { let number = "T-H-R-E-E"; // don't change this line println!("Spell a Number : {}", number); - number = 3; // don't rename this variable + let number = 3; // don't rename this variable println!("Number plus two is : {}", number + 2); } diff --git a/exercises/variables/variables6.rs b/exercises/variables/variables6.rs index a8520122c5..efd36bdfc9 100644 --- a/exercises/variables/variables6.rs +++ b/exercises/variables/variables6.rs @@ -3,7 +3,7 @@ // I AM NOT DONE -const NUMBER = 3; +const NUMBER: u32 = 3; fn main() { println!("Number {}", NUMBER); }