+
Greetings and welcome to `rustlings`. This project contains small exercises to get you used to reading and writing Rust code. This includes reading and responding to compiler messages!
_...looking for the old, web-based version of Rustlings? Try [here](https://github.com/rust-lang/rustlings/tree/rustlings-1)_
@@ -14,7 +18,7 @@ Alternatively, for a first-time Rust learner, there are several other resources:
_Note: If you're on MacOS, make sure you've installed Xcode and its developer tools by typing `xcode-select --install`._
_Note: If you're on Linux, make sure you've installed gcc. Deb: `sudo apt install gcc`. Yum: `sudo yum -y install gcc`._
-You will need to have Rust installed. You can get it by visiting https://rustup.rs. This'll also install Cargo, Rust's package/project manager.
+You will need to have Rust installed. You can get it by visiting . This'll also install Cargo, Rust's package/project manager.
## MacOS/Linux
@@ -23,6 +27,7 @@ Just run:
```bash
curl -L https://raw.githubusercontent.com/rust-lang/rustlings/main/install.sh | bash
```
+
Or if you want it to be installed to a different path:
```bash
@@ -36,8 +41,8 @@ This will install Rustlings and give you access to the `rustlings` command. Run
Basically: Clone the repository at the latest tag, finally run `nix develop` or `nix-shell`.
```bash
-# find out the latest version at https://github.com/rust-lang/rustlings/releases/latest (on edit 5.4.1)
-git clone -b 5.4.1 --depth 1 https://github.com/rust-lang/rustlings
+# find out the latest version at https://github.com/rust-lang/rustlings/releases/latest (on edit 5.5.1)
+git clone -b 5.5.1 --depth 1 https://github.com/rust-lang/rustlings
cd rustlings
# if nix version > 2.3
nix develop
@@ -74,8 +79,8 @@ If you get a permission denied message, you might have to exclude the directory
Basically: Clone the repository at the latest tag, run `cargo install --path .`.
```bash
-# find out the latest version at https://github.com/rust-lang/rustlings/releases/latest (on edit 5.4.1)
-git clone -b 5.4.1 --depth 1 https://github.com/rust-lang/rustlings
+# find out the latest version at https://github.com/rust-lang/rustlings/releases/latest (on edit 5.5.1)
+git clone -b 5.5.1 --depth 1 https://github.com/rust-lang/rustlings
cd rustlings
cargo install --force --path .
```
diff --git a/exercises/clippy/clippy1.rs b/exercises/clippy/clippy1.rs
index bad4689..95c0141 100644
--- a/exercises/clippy/clippy1.rs
+++ b/exercises/clippy/clippy1.rs
@@ -1,10 +1,13 @@
// clippy1.rs
-// The Clippy tool is a collection of lints to analyze your code
-// so you can catch common mistakes and improve your Rust code.
//
-// For these exercises the code will fail to compile when there are clippy warnings
-// check clippy's suggestions from the output to solve the exercise.
-// Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a hint.
+// The Clippy tool is a collection of lints to analyze your code so you can
+// catch common mistakes and improve your Rust code.
+//
+// For these exercises the code will fail to compile when there are clippy
+// warnings check clippy's suggestions from the output to solve the exercise.
+//
+// Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/clippy/clippy2.rs b/exercises/clippy/clippy2.rs
index dac40db..9b87a0b 100644
--- a/exercises/clippy/clippy2.rs
+++ b/exercises/clippy/clippy2.rs
@@ -1,5 +1,7 @@
// clippy2.rs
-// Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/clippy/clippy3.rs b/exercises/clippy/clippy3.rs
index b0159eb..35021f8 100644
--- a/exercises/clippy/clippy3.rs
+++ b/exercises/clippy/clippy3.rs
@@ -1,5 +1,8 @@
// clippy3.rs
+//
// Here's a couple more easy Clippy fixes, so you can see its utility.
+//
+// Execute `rustlings hint clippy3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
diff --git a/exercises/conversions/as_ref_mut.rs b/exercises/conversions/as_ref_mut.rs
index e6a9d11..626a36c 100644
--- a/exercises/conversions/as_ref_mut.rs
+++ b/exercises/conversions/as_ref_mut.rs
@@ -1,7 +1,11 @@
-// AsRef and AsMut allow for cheap reference-to-reference conversions.
-// Read more about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html
-// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
-// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint.
+// as_ref_mut.rs
+//
+// AsRef and AsMut allow for cheap reference-to-reference conversions. Read more
+// about them at https://doc.rust-lang.org/std/convert/trait.AsRef.html and
+// https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
+//
+// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/conversions/from_into.rs b/exercises/conversions/from_into.rs
index 6c272c3..60911f3 100644
--- a/exercises/conversions/from_into.rs
+++ b/exercises/conversions/from_into.rs
@@ -1,7 +1,11 @@
-// The From trait is used for value-to-value conversions.
-// If From is implemented correctly for a type, the Into trait should work conversely.
-// You can read more about it at https://doc.rust-lang.org/std/convert/trait.From.html
-// Execute `rustlings hint from_into` or use the `hint` watch subcommand for a hint.
+// from_into.rs
+//
+// The From trait is used for value-to-value conversions. If From is implemented
+// correctly for a type, the Into trait should work conversely. You can read
+// more about it at https://doc.rust-lang.org/std/convert/trait.From.html
+//
+// Execute `rustlings hint from_into` or use the `hint` watch subcommand for a
+// hint.
#[derive(Debug)]
struct Person {
@@ -20,20 +24,21 @@ impl Default for Person {
}
}
-// Your task is to complete this implementation
-// in order for the line `let p = Person::from("Mark,20")` to compile
-// Please note that you'll need to parse the age component into a `usize`
-// with something like `"4".parse::()`. The outcome of this needs to
-// be handled appropriately.
+// Your task is to complete this implementation in order for the line `let p =
+// Person::from("Mark,20")` to compile Please note that you'll need to parse the
+// age component into a `usize` with something like `"4".parse::()`. The
+// outcome of this needs to be handled appropriately.
//
// Steps:
-// 1. If the length of the provided string is 0, then return the default of Person
-// 2. Split the given string on the commas present in it
-// 3. Extract the first element from the split operation and use it as the name
-// 4. If the name is empty, then return the default of Person
-// 5. Extract the other element from the split operation and parse it into a `usize` as the age
-// If while parsing the age, something goes wrong, then return the default of Person
-// Otherwise, then return an instantiated Person object with the results
+// 1. If the length of the provided string is 0, then return the default of
+// Person.
+// 2. Split the given string on the commas present in it.
+// 3. Extract the first element from the split operation and use it as the name.
+// 4. If the name is empty, then return the default of Person.
+// 5. Extract the other element from the split operation and parse it into a
+// `usize` as the age.
+// If while parsing the age, something goes wrong, then return the default of
+// Person Otherwise, then return an instantiated Person object with the results
// I AM NOT DONE
@@ -77,7 +82,8 @@ mod tests {
}
#[test]
fn test_bad_age() {
- // Test that "Mark,twenty" will return the default person due to an error in parsing age
+ // Test that "Mark,twenty" will return the default person due to an
+ // error in parsing age
let p = Person::from("Mark,twenty");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
@@ -121,14 +127,14 @@ mod tests {
#[test]
fn test_trailing_comma() {
let p: Person = Person::from("Mike,32,");
- assert_eq!(p.name, "John");
- assert_eq!(p.age, 30);
+ assert_eq!(p.name, "Mike");
+ assert_eq!(p.age, 32);
}
#[test]
fn test_trailing_comma_and_some_string() {
let p: Person = Person::from("Mike,32,man");
- assert_eq!(p.name, "John");
- assert_eq!(p.age, 30);
+ assert_eq!(p.name, "Mike");
+ assert_eq!(p.age, 32);
}
}
diff --git a/exercises/conversions/from_str.rs b/exercises/conversions/from_str.rs
index fe16815..34472c3 100644
--- a/exercises/conversions/from_str.rs
+++ b/exercises/conversions/from_str.rs
@@ -1,10 +1,13 @@
// from_str.rs
-// This is similar to from_into.rs, but this time we'll implement `FromStr`
-// and return errors instead of falling back to a default value.
-// Additionally, upon implementing FromStr, you can use the `parse` method
-// on strings to generate an object of the implementor type.
-// You can read more about it at https://doc.rust-lang.org/std/str/trait.FromStr.html
-// Execute `rustlings hint from_str` or use the `hint` watch subcommand for a hint.
+//
+// This is similar to from_into.rs, but this time we'll implement `FromStr` and
+// return errors instead of falling back to a default value. Additionally, upon
+// implementing FromStr, you can use the `parse` method on strings to generate
+// an object of the implementor type. You can read more about it at
+// https://doc.rust-lang.org/std/str/trait.FromStr.html
+//
+// Execute `rustlings hint from_str` or use the `hint` watch subcommand for a
+// hint.
use std::num::ParseIntError;
use std::str::FromStr;
@@ -33,15 +36,18 @@ enum ParsePersonError {
// Steps:
// 1. If the length of the provided string is 0, an error should be returned
// 2. Split the given string on the commas present in it
-// 3. Only 2 elements should be returned from the split, otherwise return an error
+// 3. Only 2 elements should be returned from the split, otherwise return an
+// error
// 4. Extract the first element from the split operation and use it as the name
-// 5. Extract the other element from the split operation and parse it into a `usize` as the age
-// with something like `"4".parse::()`
-// 6. If while extracting the name and the age something goes wrong, an error should be returned
+// 5. Extract the other element from the split operation and parse it into a
+// `usize` as the age with something like `"4".parse::()`
+// 6. If while extracting the name and the age something goes wrong, an error
+// should be returned
// If everything goes well, then return a Result of a Person object
//
-// As an aside: `Box` implements `From<&'_ str>`. This means that if you want to return a
-// string error message, you can do so via just using return `Err("my error message".into())`.
+// As an aside: `Box` implements `From<&'_ str>`. This means that if
+// you want to return a string error message, you can do so via just using
+// return `Err("my error message".into())`.
impl FromStr for Person {
type Err = ParsePersonError;
diff --git a/exercises/conversions/try_from_into.rs b/exercises/conversions/try_from_into.rs
index fa98bc9..32d6ef3 100644
--- a/exercises/conversions/try_from_into.rs
+++ b/exercises/conversions/try_from_into.rs
@@ -1,9 +1,13 @@
// try_from_into.rs
-// TryFrom is a simple and safe type conversion that may fail in a controlled way under some circumstances.
-// Basically, this is the same as From. The main difference is that this should return a Result type
-// instead of the target type itself.
-// You can read more about it at https://doc.rust-lang.org/std/convert/trait.TryFrom.html
-// Execute `rustlings hint try_from_into` or use the `hint` watch subcommand for a hint.
+//
+// TryFrom is a simple and safe type conversion that may fail in a controlled
+// way under some circumstances. Basically, this is the same as From. The main
+// difference is that this should return a Result type instead of the target
+// type itself. You can read more about it at
+// https://doc.rust-lang.org/std/convert/trait.TryFrom.html
+//
+// Execute `rustlings hint try_from_into` or use the `hint` watch subcommand for
+// a hint.
use std::convert::{TryFrom, TryInto};
@@ -25,14 +29,13 @@ enum IntoColorError {
// I AM NOT DONE
-// Your task is to complete this implementation
-// and return an Ok result of inner type Color.
-// You need to create an implementation for a tuple of three integers,
-// an array of three integers, and a slice of integers.
+// Your task is to complete this implementation and return an Ok result of inner
+// type Color. You need to create an implementation for a tuple of three
+// integers, an array of three integers, and a slice of integers.
//
-// Note that the implementation for tuple and array will be checked at compile time,
-// but the slice implementation needs to check the slice length!
-// Also note that correct RGB color values must be integers in the 0..=255 range.
+// Note that the implementation for tuple and array will be checked at compile
+// time, but the slice implementation needs to check the slice length! Also note
+// that correct RGB color values must be integers in the 0..=255 range.
// Tuple implementation
impl TryFrom<(i16, i16, i16)> for Color {
diff --git a/exercises/conversions/using_as.rs b/exercises/conversions/using_as.rs
index 8c9b711..414cef3 100644
--- a/exercises/conversions/using_as.rs
+++ b/exercises/conversions/using_as.rs
@@ -1,10 +1,14 @@
-// Type casting in Rust is done via the usage of the `as` operator.
-// Please note that the `as` operator is not only used when type casting.
-// It also helps with renaming imports.
+// using_as.rs
//
-// The goal is to make sure that the division does not fail to compile
-// and returns the proper type.
-// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a hint.
+// Type casting in Rust is done via the usage of the `as` operator. Please note
+// that the `as` operator is not only used when type casting. It also helps with
+// renaming imports.
+//
+// The goal is to make sure that the division does not fail to compile and
+// returns the proper type.
+//
+// Execute `rustlings hint using_as` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/enums/enums1.rs b/exercises/enums/enums1.rs
index 511ba74..25525b2 100644
--- a/exercises/enums/enums1.rs
+++ b/exercises/enums/enums1.rs
@@ -1,4 +1,5 @@
// enums1.rs
+//
// No hints this time! ;)
// I AM NOT DONE
diff --git a/exercises/enums/enums2.rs b/exercises/enums/enums2.rs
index 167a6b2..df93fe0 100644
--- a/exercises/enums/enums2.rs
+++ b/exercises/enums/enums2.rs
@@ -1,5 +1,7 @@
// enums2.rs
-// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/enums/enums3.rs b/exercises/enums/enums3.rs
index a2a9d58..98da169 100644
--- a/exercises/enums/enums3.rs
+++ b/exercises/enums/enums3.rs
@@ -1,6 +1,9 @@
// enums3.rs
+//
// Address all the TODOs to make the tests pass!
-// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
@@ -17,6 +20,7 @@ struct State {
color: (u8, u8, u8),
position: Point,
quit: bool,
+ message: String
}
impl State {
@@ -28,17 +32,17 @@ impl State {
self.quit = true;
}
- fn echo(&self, s: String) {
- println!("{}", s);
- }
+ fn echo(&mut self, s: String) { self.message = s }
fn move_position(&mut self, p: Point) {
self.position = p;
}
fn process(&mut self, message: Message) {
- // TODO: create a match expression to process the different message variants
- // Remember: When passing a tuple as a function argument, you'll need extra parentheses: fn function((t, u, p, l, e))
+ // TODO: create a match expression to process the different message
+ // variants
+ // Remember: When passing a tuple as a function argument, you'll need
+ // extra parentheses: fn function((t, u, p, l, e))
}
}
@@ -52,9 +56,10 @@ mod tests {
quit: false,
position: Point { x: 0, y: 0 },
color: (0, 0, 0),
+ message: "hello world".to_string(),
};
state.process(Message::ChangeColor(255, 0, 255));
- state.process(Message::Echo(String::from("hello world")));
+ state.process(Message::Echo(String::from("Hello world!")));
state.process(Message::Move(Point { x: 10, y: 15 }));
state.process(Message::Quit);
@@ -62,5 +67,6 @@ mod tests {
assert_eq!(state.position.x, 10);
assert_eq!(state.position.y, 15);
assert_eq!(state.quit, true);
+ assert_eq!(state.message, "Hello world!");
}
}
diff --git a/exercises/error_handling/errors1.rs b/exercises/error_handling/errors1.rs
index bcee972..0ba59a5 100644
--- a/exercises/error_handling/errors1.rs
+++ b/exercises/error_handling/errors1.rs
@@ -1,9 +1,13 @@
// errors1.rs
-// This function refuses to generate text to be printed on a nametag if
-// you pass it an empty string. It'd be nicer if it explained what the problem
-// was, instead of just sometimes returning `None`. Thankfully, Rust has a similar
-// 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.
+//
+// This function refuses to generate text to be printed on a nametag if you pass
+// it an empty string. It'd be nicer if it explained what the problem was,
+// instead of just sometimes returning `None`. Thankfully, Rust has a similar
+// 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
diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs
index 6971fcf..d4a5477 100644
--- a/exercises/error_handling/errors2.rs
+++ b/exercises/error_handling/errors2.rs
@@ -1,21 +1,23 @@
// errors2.rs
+//
// Say we're writing a game where you can buy items with tokens. All items cost
// 5 tokens, and whenever you purchase items there is a processing fee of 1
-// token. A player of the game will type in how many items they want to buy,
-// and the `total_cost` function will calculate the total cost of the tokens.
-// Since the player typed in the quantity, though, we get it as a string-- and
-// they might have typed anything, not just numbers!
-
+// token. A player of the game will type in how many items they want to buy, and
+// the `total_cost` function will calculate the total cost of the tokens. Since
+// the player typed in the quantity, though, we get it as a string-- and they
+// might have typed anything, not just numbers!
+//
// Right now, this function isn't handling the error case at all (and isn't
-// handling the success case properly either). What we want to do is:
-// if we call the `parse` function on a string that is not a number, that
-// function will return a `ParseIntError`, and in that case, we want to
-// immediately return that error from our function and not try to multiply
-// and add.
-
-// There are at least two ways to implement this that are both correct-- but
-// one is a lot shorter!
-// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a hint.
+// handling the success case properly either). What we want to do is: if we call
+// the `total_cost` function on a string that is not a number, that function
+// will return a `ParseIntError`, and in that case, we want to immediately
+// return that error from our function and not try to multiply and add.
+//
+// There are at least two ways to implement this that are both correct-- but one
+// is a lot shorter!
+//
+// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/error_handling/errors3.rs b/exercises/error_handling/errors3.rs
index a2d2d19..d42d3b1 100644
--- a/exercises/error_handling/errors3.rs
+++ b/exercises/error_handling/errors3.rs
@@ -1,8 +1,11 @@
// errors3.rs
+//
// This is a program that is trying to use a completed version of the
// `total_cost` function from the previous exercise. It's not working though!
// Why not? What should we do to fix it?
-// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs
index 0efe8cc..d6d6fcb 100644
--- a/exercises/error_handling/errors4.rs
+++ b/exercises/error_handling/errors4.rs
@@ -1,5 +1,7 @@
// errors4.rs
-// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
@@ -14,7 +16,7 @@ enum CreationError {
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result {
- // Hmm...? Why is this only returning an Ok value?
+ // Hmm... Why is this always returning an Ok value?
Ok(PositiveNonzeroInteger(value as u64))
}
}
diff --git a/exercises/error_handling/errors5.rs b/exercises/error_handling/errors5.rs
index eb5506c..92461a7 100644
--- a/exercises/error_handling/errors5.rs
+++ b/exercises/error_handling/errors5.rs
@@ -1,20 +1,26 @@
// errors5.rs
-
+//
// This program uses an altered version of the code from errors4.
-
-// This exercise uses some concepts that we won't get to until later in the course, like `Box` and the
-// `From` trait. It's not important to understand them in detail right now, but you can read ahead if you like.
-// For now, think of the `Box` type as an "I want anything that does ???" type, which, given
-// Rust's usual standards for runtime safety, should strike you as somewhat lenient!
-
-// In short, this particular use case for boxes is for when you want to own a value and you care only that it is a
-// type which implements a particular trait. To do so, The Box is declared as of type Box where Trait is the trait
-// the compiler looks for on any value used in that context. For this exercise, that context is the potential errors
-// which can be returned in a Result.
-
-// What can we use to describe both errors? In other words, is there a trait which both errors implement?
-
-// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a hint.
+//
+// This exercise uses some concepts that we won't get to until later in the
+// course, like `Box` and the `From` trait. It's not important to understand
+// them in detail right now, but you can read ahead if you like. For now, think
+// of the `Box` type as an "I want anything that does ???" type, which,
+// given Rust's usual standards for runtime safety, should strike you as
+// somewhat lenient!
+//
+// In short, this particular use case for boxes is for when you want to own a
+// value and you care only that it is a type which implements a particular
+// trait. To do so, The Box is declared as of type Box where Trait is
+// the trait the compiler looks for on any value used in that context. For this
+// exercise, that context is the potential errors which can be returned in a
+// Result.
+//
+// What can we use to describe both errors? In other words, is there a trait
+// which both errors implement?
+//
+// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/error_handling/errors6.rs b/exercises/error_handling/errors6.rs
index 8097b49..aaf0948 100644
--- a/exercises/error_handling/errors6.rs
+++ b/exercises/error_handling/errors6.rs
@@ -1,12 +1,13 @@
// errors6.rs
-
+//
// Using catch-all error types like `Box` isn't recommended
// for library code, where callers might want to make decisions based on the
-// error content, instead of printing it out or propagating it further. Here,
-// we define a custom error type to make it possible for callers to decide
-// what to do next when our function returns an error.
-
-// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint.
+// error content, instead of printing it out or propagating it further. Here, we
+// define a custom error type to make it possible for callers to decide what to
+// do next when our function returns an error.
+//
+// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/functions/functions1.rs b/exercises/functions/functions1.rs
index 03d8af7..40ed9a0 100644
--- a/exercises/functions/functions1.rs
+++ b/exercises/functions/functions1.rs
@@ -1,5 +1,7 @@
// functions1.rs
-// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/functions/functions2.rs b/exercises/functions/functions2.rs
index 7d40a57..5154f34 100644
--- a/exercises/functions/functions2.rs
+++ b/exercises/functions/functions2.rs
@@ -1,5 +1,7 @@
// functions2.rs
-// Execute `rustlings hint functions2` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint functions2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/functions/functions3.rs b/exercises/functions/functions3.rs
index 3b9e585..74f44d6 100644
--- a/exercises/functions/functions3.rs
+++ b/exercises/functions/functions3.rs
@@ -1,5 +1,7 @@
// functions3.rs
-// Execute `rustlings hint functions3` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint functions3` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/functions/functions4.rs b/exercises/functions/functions4.rs
index 65d5be4..77c4b2a 100644
--- a/exercises/functions/functions4.rs
+++ b/exercises/functions/functions4.rs
@@ -1,11 +1,12 @@
// functions4.rs
-// Execute `rustlings hint functions4` or use the `hint` watch subcommand for a hint.
-
-// This store is having a sale where if the price is an even number, you get
-// 10 Rustbucks off, but if it's an odd number, it's 3 Rustbucks off.
-// (Don't worry about the function bodies themselves, we're only interested
-// in the signatures for now. If anything, this is a good way to peek ahead
-// to future exercises!)
+//
+// This store is having a sale where if the price is an even number, you get 10
+// Rustbucks off, but if it's an odd number, it's 3 Rustbucks off. (Don't worry
+// about the function bodies themselves, we're only interested in the signatures
+// for now. If anything, this is a good way to peek ahead to future exercises!)
+//
+// Execute `rustlings hint functions4` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/functions/functions5.rs b/exercises/functions/functions5.rs
index 5d76296..f1b63f4 100644
--- a/exercises/functions/functions5.rs
+++ b/exercises/functions/functions5.rs
@@ -1,5 +1,7 @@
// functions5.rs
-// Execute `rustlings hint functions5` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint functions5` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/generics/generics1.rs b/exercises/generics/generics1.rs
index 4c34ae4..35c1d2f 100644
--- a/exercises/generics/generics1.rs
+++ b/exercises/generics/generics1.rs
@@ -1,7 +1,10 @@
-// This shopping list program isn't compiling!
-// Use your knowledge of generics to fix it.
-
-// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a hint.
+// generics1.rs
+//
+// This shopping list program isn't compiling! Use your knowledge of generics to
+// fix it.
+//
+// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/generics/generics2.rs b/exercises/generics/generics2.rs
index aedbd55..074cd93 100644
--- a/exercises/generics/generics2.rs
+++ b/exercises/generics/generics2.rs
@@ -1,7 +1,10 @@
+// generics2.rs
+//
// This powerful wrapper provides the ability to store a positive integer value.
// Rewrite it using generics so that it supports wrapping ANY type.
-
-// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs
index fd8dd2f..80829ea 100644
--- a/exercises/hashmaps/hashmaps1.rs
+++ b/exercises/hashmaps/hashmaps1.rs
@@ -1,14 +1,15 @@
// hashmaps1.rs
-// A basket of fruits in the form of a hash map needs to be defined.
-// The key represents the name of the fruit and the value represents
-// how many of that particular fruit is in the basket. You have to put
-// at least three different types of fruits (e.g apple, banana, mango)
-// in the basket and the total count of all the fruits should be at
-// least five.
+//
+// A basket of fruits in the form of a hash map needs to be defined. The key
+// represents the name of the fruit and the value represents how many of that
+// particular fruit is in the basket. You have to put at least three different
+// types of fruits (e.g apple, banana, mango) in the basket and the total count
+// of all the fruits should be at least five.
//
// Make me compile and pass the tests!
//
-// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a hint.
+// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs
index a4f069a..a592569 100644
--- a/exercises/hashmaps/hashmaps2.rs
+++ b/exercises/hashmaps/hashmaps2.rs
@@ -1,17 +1,18 @@
// hashmaps2.rs
-// We're collecting different fruits to bake a delicious fruit cake.
-// For this, we have a basket, which we'll represent in the form of a hash
-// map. The key represents the name of each fruit we collect and the value
-// represents how many of that particular fruit we have collected.
-// Three types of fruits - Apple (4), Mango (2) and Lychee (5) are already
-// in the basket hash map.
-// You must add fruit to the basket so that there is at least
-// one of each kind and more than 11 in total - we have a lot of mouths to feed.
-// You are not allowed to insert any more of these fruits!
+//
+// We're collecting different fruits to bake a delicious fruit cake. For this,
+// we have a basket, which we'll represent in the form of a hash map. The key
+// represents the name of each fruit we collect and the value represents how
+// many of that particular fruit we have collected. Three types of fruits -
+// Apple (4), Mango (2) and Lychee (5) are already in the basket hash map. You
+// must add fruit to the basket so that there is at least one of each kind and
+// more than 11 in total - we have a lot of mouths to feed. You are not allowed
+// to insert any more of these fruits!
//
// Make me pass the tests!
//
-// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint.
+// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
@@ -36,9 +37,9 @@ fn fruit_basket(basket: &mut HashMap) {
];
for fruit in fruit_kinds {
- // TODO: Insert new fruits if they are not already present in the basket.
- // Note that you are not allowed to put any type of fruit that's already
- // present!
+ // TODO: Insert new fruits if they are not already present in the
+ // basket. Note that you are not allowed to put any type of fruit that's
+ // already present!
}
}
@@ -80,4 +81,13 @@ mod tests {
let count = basket.values().sum::();
assert!(count > 11);
}
+
+ #[test]
+ fn all_fruit_types_in_basket() {
+ let mut basket = get_fruit_basket();
+ fruit_basket(&mut basket);
+ for amount in basket.values() {
+ assert_ne!(amount, &0);
+ }
+ }
}
diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs
index ad3baa6..08e977c 100644
--- a/exercises/hashmaps/hashmaps3.rs
+++ b/exercises/hashmaps/hashmaps3.rs
@@ -1,26 +1,25 @@
// hashmaps3.rs
-
-// A list of scores (one per line) of a soccer match is given. Each line
-// is of the form :
-// ,,,
+//
+// A list of scores (one per line) of a soccer match is given. Each line is of
+// the form : ",,,"
// Example: England,France,4,2 (England scored 4 goals, France 2).
-
-// You have to build a scores table containing the name of the team, goals
-// the team scored, and goals the team conceded. One approach to build
-// the scores table is to use a Hashmap. The solution is partially
-// written to use a Hashmap, complete it to pass the test.
-
+//
+// You have to build a scores table containing the name of the team, goals the
+// team scored, and goals the team conceded. One approach to build the scores
+// table is to use a Hashmap. The solution is partially written to use a
+// Hashmap, complete it to pass the test.
+//
// Make me pass the tests!
-
-// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
use std::collections::HashMap;
-// A structure to store team name and its goal details.
+// A structure to store the goal details of a team.
struct Team {
- name: String,
goals_scored: u8,
goals_conceded: u8,
}
diff --git a/exercises/if/if1.rs b/exercises/if/if1.rs
index 587e03f..4734d78 100644
--- a/exercises/if/if1.rs
+++ b/exercises/if/if1.rs
@@ -1,4 +1,5 @@
// if1.rs
+//
// Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
@@ -24,4 +25,9 @@ mod tests {
fn fortytwo_is_bigger_than_thirtytwo() {
assert_eq!(42, bigger(32, 42));
}
+
+ #[test]
+ fn equal_numbers() {
+ assert_eq!(42, bigger(42, 42));
+ }
}
diff --git a/exercises/if/if2.rs b/exercises/if/if2.rs
index effddbb..f512f13 100644
--- a/exercises/if/if2.rs
+++ b/exercises/if/if2.rs
@@ -1,7 +1,8 @@
// if2.rs
-
+//
// Step 1: Make me compile!
// Step 2: Get the bar_for_fuzz and default_to_baz tests passing!
+//
// Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
diff --git a/exercises/if/if3.rs b/exercises/if/if3.rs
new file mode 100644
index 0000000..1696274
--- /dev/null
+++ b/exercises/if/if3.rs
@@ -0,0 +1,56 @@
+// if3.rs
+//
+// Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint.
+
+// I AM NOT DONE
+
+pub fn animal_habitat(animal: &str) -> &'static str {
+ let identifier = if animal == "crab" {
+ 1
+ } else if animal == "gopher" {
+ 2.0
+ } else if animal == "snake" {
+ 3
+ } else {
+ "Unknown"
+ };
+
+ // DO NOT CHANGE THIS STATEMENT BELOW
+ let habitat = if identifier == 1 {
+ "Beach"
+ } else if identifier == 2 {
+ "Burrow"
+ } else if identifier == 3 {
+ "Desert"
+ } else {
+ "Unknown"
+ };
+
+ habitat
+}
+
+// No test changes needed.
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn gopher_lives_in_burrow() {
+ assert_eq!(animal_habitat("gopher"), "Burrow")
+ }
+
+ #[test]
+ fn snake_lives_in_desert() {
+ assert_eq!(animal_habitat("snake"), "Desert")
+ }
+
+ #[test]
+ fn crab_lives_on_beach() {
+ assert_eq!(animal_habitat("crab"), "Beach")
+ }
+
+ #[test]
+ fn unknown_animal() {
+ assert_eq!(animal_habitat("dinosaur"), "Unknown")
+ }
+}
diff --git a/exercises/intro/intro1.rs b/exercises/intro/intro1.rs
index cfc55c3..37fa011 100644
--- a/exercises/intro/intro1.rs
+++ b/exercises/intro/intro1.rs
@@ -1,13 +1,17 @@
// intro1.rs
+//
// About this `I AM NOT DONE` thing:
// We sometimes encourage you to keep trying things on a given exercise, even
// after you already figured it out. If you got everything working and feel
// ready for the next exercise, remove the `I AM NOT DONE` comment below.
-// Execute `rustlings hint intro1` or use the `hint` watch subcommand for a hint.
//
-// If you're running this using `rustlings watch`: The exercise file will be reloaded
-// when you change one of the lines below! Try adding a `println!` line, or try changing
-// what it outputs in your terminal. Try removing a semicolon and see what happens!
+// If you're running this using `rustlings watch`: The exercise file will be
+// reloaded when you change one of the lines below! Try adding a `println!`
+// line, or try changing what it outputs in your terminal. Try removing a
+// semicolon and see what happens!
+//
+// Execute `rustlings hint intro1` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/intro/intro2.rs b/exercises/intro/intro2.rs
index efc1af2..990b20f 100644
--- a/exercises/intro/intro2.rs
+++ b/exercises/intro/intro2.rs
@@ -1,6 +1,9 @@
// intro2.rs
+//
// Make the code print a greeting to the world.
-// Execute `rustlings hint intro2` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint intro2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/iterators/iterators1.rs b/exercises/iterators/iterators1.rs
index f9cc3b3..b3f698b 100644
--- a/exercises/iterators/iterators1.rs
+++ b/exercises/iterators/iterators1.rs
@@ -1,12 +1,13 @@
// iterators1.rs
//
-// Make me compile by filling in the `???`s
+// When performing operations on elements within a collection, iterators are
+// essential. This module helps you get familiar with the structure of using an
+// iterator and how to go through elements within an iterable collection.
//
-// When performing operations on elements within a collection, iterators are essential.
-// This module helps you get familiar with the structure of using an iterator and
-// how to go through elements within an iterable collection.
+// Make me compile by filling in the `???`s
//
-// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a hint.
+// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/iterators/iterators2.rs b/exercises/iterators/iterators2.rs
index 29c53af..dda82a0 100644
--- a/exercises/iterators/iterators2.rs
+++ b/exercises/iterators/iterators2.rs
@@ -1,7 +1,10 @@
// iterators2.rs
+//
// In this exercise, you'll learn some of the unique advantages that iterators
// can offer. Follow the steps to complete the exercise.
-// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/iterators/iterators3.rs b/exercises/iterators/iterators3.rs
index c97a625..29fa23a 100644
--- a/exercises/iterators/iterators3.rs
+++ b/exercises/iterators/iterators3.rs
@@ -1,10 +1,13 @@
// iterators3.rs
-// This is a bigger exercise than most of the others! You can do it!
-// Here is your mission, should you choose to accept it:
+//
+// This is a bigger exercise than most of the others! You can do it! Here is
+// your mission, should you choose to accept it:
// 1. Complete the divide function to get the first four tests to pass.
// 2. Get the remaining tests to pass by completing the result_with_list and
// list_of_results functions.
-// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
@@ -26,14 +29,16 @@ pub fn divide(a: i32, b: i32) -> Result {
todo!();
}
-// Complete the function and return a value of the correct type so the test passes.
+// Complete the function and return a value of the correct type so the test
+// passes.
// Desired output: Ok([1, 11, 1426, 3])
fn result_with_list() -> () {
let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
}
-// Complete the function and return a value of the correct type so the test passes.
+// Complete the function and return a value of the correct type so the test
+// passes.
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
fn list_of_results() -> () {
let numbers = vec![27, 297, 38502, 81];
diff --git a/exercises/iterators/iterators4.rs b/exercises/iterators/iterators4.rs
index a02470e..79e1692 100644
--- a/exercises/iterators/iterators4.rs
+++ b/exercises/iterators/iterators4.rs
@@ -1,5 +1,7 @@
// iterators4.rs
-// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/iterators/iterators5.rs b/exercises/iterators/iterators5.rs
index 8709795..a062ee4 100644
--- a/exercises/iterators/iterators5.rs
+++ b/exercises/iterators/iterators5.rs
@@ -1,4 +1,5 @@
// iterators5.rs
+//
// Let's define a simple model to track Rustlings exercise progress. Progress
// will be modelled using a hash map. The name of the exercise is the key and
// the progress is the value. Two counting functions were created to count the
@@ -6,7 +7,9 @@
// functionality using iterators. Try not to use imperative loops (for, while).
// Only the two iterator methods (count_iterator and count_collection_iterator)
// need to be modified.
-// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
@@ -65,12 +68,27 @@ mod tests {
}
#[test]
- fn count_equals_for() {
+ fn count_some() {
let map = get_map();
- assert_eq!(
- count_for(&map, Progress::Complete),
- count_iterator(&map, Progress::Complete)
- );
+ assert_eq!(1, count_iterator(&map, Progress::Some));
+ }
+
+ #[test]
+ fn count_none() {
+ let map = get_map();
+ assert_eq!(2, count_iterator(&map, Progress::None));
+ }
+
+ #[test]
+ fn count_complete_equals_for() {
+ let map = get_map();
+ let progress_states = vec![Progress::Complete, Progress::Some, Progress::None];
+ for progress_state in progress_states {
+ assert_eq!(
+ count_for(&map, progress_state),
+ count_iterator(&map, progress_state)
+ );
+ }
}
#[test]
@@ -83,12 +101,28 @@ mod tests {
}
#[test]
- fn count_collection_equals_for() {
+ fn count_collection_some() {
let collection = get_vec_map();
- assert_eq!(
- count_collection_for(&collection, Progress::Complete),
- count_collection_iterator(&collection, Progress::Complete)
- );
+ assert_eq!(1, count_collection_iterator(&collection, Progress::Some));
+ }
+
+ #[test]
+ fn count_collection_none() {
+ let collection = get_vec_map();
+ assert_eq!(4, count_collection_iterator(&collection, Progress::None));
+ }
+
+ #[test]
+ fn count_collection_equals_for() {
+ let progress_states = vec![Progress::Complete, Progress::Some, Progress::None];
+ let collection = get_vec_map();
+
+ for progress_state in progress_states {
+ assert_eq!(
+ count_collection_for(&collection, progress_state),
+ count_collection_iterator(&collection, progress_state)
+ );
+ }
}
fn get_map() -> HashMap {
diff --git a/exercises/lifetimes/lifetimes1.rs b/exercises/lifetimes/lifetimes1.rs
index 0236470..87bde49 100644
--- a/exercises/lifetimes/lifetimes1.rs
+++ b/exercises/lifetimes/lifetimes1.rs
@@ -1,11 +1,12 @@
// lifetimes1.rs
//
// The Rust compiler needs to know how to check whether supplied references are
-// valid, so that it can let the programmer know if a reference is at risk
-// of going out of scope before it is used. Remember, references are borrows
-// and do not own their own data. What if their owner goes out of scope?
+// valid, so that it can let the programmer know if a reference is at risk of
+// going out of scope before it is used. Remember, references are borrows and do
+// not own their own data. What if their owner goes out of scope?
//
-// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a hint.
+// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/lifetimes/lifetimes2.rs b/exercises/lifetimes/lifetimes2.rs
index b48feab..4f3d8c1 100644
--- a/exercises/lifetimes/lifetimes2.rs
+++ b/exercises/lifetimes/lifetimes2.rs
@@ -1,10 +1,10 @@
// lifetimes2.rs
//
-// So if the compiler is just validating the references passed
-// to the annotated parameters and the return type, what do
-// we need to change?
+// So if the compiler is just validating the references passed to the annotated
+// parameters and the return type, what do we need to change?
//
-// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a hint.
+// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/lifetimes/lifetimes3.rs b/exercises/lifetimes/lifetimes3.rs
index ea48370..9c59f9c 100644
--- a/exercises/lifetimes/lifetimes3.rs
+++ b/exercises/lifetimes/lifetimes3.rs
@@ -2,7 +2,8 @@
//
// Lifetimes are also needed when structs hold references.
//
-// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a hint.
+// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/macros/macros1.rs b/exercises/macros/macros1.rs
index 634d0a7..678de6e 100644
--- a/exercises/macros/macros1.rs
+++ b/exercises/macros/macros1.rs
@@ -1,5 +1,7 @@
// macros1.rs
-// Execute `rustlings hint macros1` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint macros1` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/macros/macros2.rs b/exercises/macros/macros2.rs
index f6092ca..788fc16 100644
--- a/exercises/macros/macros2.rs
+++ b/exercises/macros/macros2.rs
@@ -1,5 +1,7 @@
// macros2.rs
-// Execute `rustlings hint macros2` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint macros2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/macros/macros3.rs b/exercises/macros/macros3.rs
index 106f1c6..b795c14 100644
--- a/exercises/macros/macros3.rs
+++ b/exercises/macros/macros3.rs
@@ -1,6 +1,9 @@
// macros3.rs
+//
// Make me compile, without taking the macro out of the module!
-// Execute `rustlings hint macros3` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint macros3` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/macros/macros4.rs b/exercises/macros/macros4.rs
index 4ee9803..71b45a0 100644
--- a/exercises/macros/macros4.rs
+++ b/exercises/macros/macros4.rs
@@ -1,5 +1,7 @@
// macros4.rs
-// Execute `rustlings hint macros4` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint macros4` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/modules/modules1.rs b/exercises/modules/modules1.rs
index 8dd0e40..9eb5a48 100644
--- a/exercises/modules/modules1.rs
+++ b/exercises/modules/modules1.rs
@@ -1,5 +1,7 @@
// modules1.rs
-// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/modules/modules2.rs b/exercises/modules/modules2.rs
index c30a389..0415454 100644
--- a/exercises/modules/modules2.rs
+++ b/exercises/modules/modules2.rs
@@ -1,7 +1,11 @@
// modules2.rs
-// You can bring module paths into scopes and provide new names for them with the
-// 'use' and 'as' keywords. Fix these 'use' statements to make the code compile.
-// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a hint.
+//
+// You can bring module paths into scopes and provide new names for them with
+// the 'use' and 'as' keywords. Fix these 'use' statements to make the code
+// compile.
+//
+// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/modules/modules3.rs b/exercises/modules/modules3.rs
index 35e0799..f2bb050 100644
--- a/exercises/modules/modules3.rs
+++ b/exercises/modules/modules3.rs
@@ -1,9 +1,12 @@
// modules3.rs
-// You can use the 'use' keyword to bring module paths from modules from anywhere
-// and especially from the Rust standard library into your scope.
-// Bring SystemTime and UNIX_EPOCH
-// from the std::time module. Bonus style points if you can do it with one line!
-// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a hint.
+//
+// You can use the 'use' keyword to bring module paths from modules from
+// anywhere and especially from the Rust standard library into your scope. Bring
+// SystemTime and UNIX_EPOCH from the std::time module. Bonus style points if
+// you can do it with one line!
+//
+// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
diff --git a/exercises/move_semantics/move_semantics1.rs b/exercises/move_semantics/move_semantics1.rs
index aac6dfc..e063937 100644
--- a/exercises/move_semantics/move_semantics1.rs
+++ b/exercises/move_semantics/move_semantics1.rs
@@ -1,26 +1,23 @@
// move_semantics1.rs
-// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand
+// for a hint.
// I AM NOT DONE
+#[test]
fn main() {
- let vec0 = Vec::new();
+ let vec0 = vec![22, 44, 66];
let vec1 = fill_vec(vec0);
- println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
-
- vec1.push(88);
-
- println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
}
fn fill_vec(vec: Vec) -> Vec {
- let mut vec = vec;
+ let vec = vec;
- vec.push(22);
- vec.push(44);
- vec.push(66);
+ vec.push(88);
vec
}
diff --git a/exercises/move_semantics/move_semantics2.rs b/exercises/move_semantics/move_semantics2.rs
index 68dbf02..baf6bcc 100644
--- a/exercises/move_semantics/move_semantics2.rs
+++ b/exercises/move_semantics/move_semantics2.rs
@@ -1,32 +1,26 @@
// move_semantics2.rs
-// 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.
-
-// Expected output:
-// vec0 has length 3 content `[22, 44, 66]`
-// vec1 has length 4 content `[22, 44, 66, 88]`
+//
+// Make the test pass by finding a way to keep both Vecs separate!
+//
+// Execute `rustlings hint move_semantics2` or use the `hint` watch subcommand
+// for a hint.
// I AM NOT DONE
+#[test]
fn main() {
- let vec0 = Vec::new();
+ let vec0 = vec![22, 44, 66];
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);
+ assert_eq!(vec0, vec![22, 44, 66]);
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
}
fn fill_vec(vec: Vec) -> Vec {
let mut vec = vec;
- vec.push(22);
- vec.push(44);
- vec.push(66);
+ vec.push(88);
vec
}
diff --git a/exercises/move_semantics/move_semantics3.rs b/exercises/move_semantics/move_semantics3.rs
index eaa30e3..69e564a 100644
--- a/exercises/move_semantics/move_semantics3.rs
+++ b/exercises/move_semantics/move_semantics3.rs
@@ -1,26 +1,24 @@
// move_semantics3.rs
-// Make me compile without adding new lines-- just changing existing lines!
-// (no lines with multiple semicolons necessary!)
-// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand for a hint.
+//
+// Make me compile without adding new lines-- just changing existing lines! (no
+// lines with multiple semicolons necessary!)
+//
+// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand
+// for a hint.
// I AM NOT DONE
+#[test]
fn main() {
- let vec0 = Vec::new();
+ let vec0 = vec![22, 44, 66];
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);
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
}
fn fill_vec(vec: Vec) -> Vec {
- vec.push(22);
- vec.push(44);
- vec.push(66);
+ vec.push(88);
vec
}
diff --git a/exercises/move_semantics/move_semantics4.rs b/exercises/move_semantics/move_semantics4.rs
index 99834ec..80b49db 100644
--- a/exercises/move_semantics/move_semantics4.rs
+++ b/exercises/move_semantics/move_semantics4.rs
@@ -1,30 +1,29 @@
// move_semantics4.rs
-// Refactor this code so that instead of passing `vec0` into the `fill_vec` function,
-// the Vector gets created in the function itself and passed back to the main
-// function.
-// Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand for a hint.
+//
+// Refactor this code so that instead of passing `vec0` into the `fill_vec`
+// function, the Vector gets created in the function itself and passed back to
+// the main function.
+//
+// Execute `rustlings hint move_semantics4` or use the `hint` watch subcommand
+// for a hint.
// I AM NOT DONE
+#[test]
fn main() {
- let vec0 = Vec::new();
+ let vec0 = vec![22, 44, 66];
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);
+ assert_eq!(vec1, vec![22, 44, 66, 88]);
}
-// `fill_vec()` no longer takes `vec: Vec` as argument
+// `fill_vec()` no longer takes `vec: Vec` as argument - don't change this!
fn fill_vec() -> Vec {
+ // Instead, let's create and fill the Vec in here - how do you do that?
let mut vec = vec;
- vec.push(22);
- vec.push(44);
- vec.push(66);
+ vec.push(88);
vec
}
diff --git a/exercises/move_semantics/move_semantics5.rs b/exercises/move_semantics/move_semantics5.rs
index 36eae12..68db09e 100644
--- a/exercises/move_semantics/move_semantics5.rs
+++ b/exercises/move_semantics/move_semantics5.rs
@@ -1,7 +1,10 @@
// move_semantics5.rs
-// Make me compile only by reordering the lines in `main()`, but without
-// adding, changing or removing any of them.
-// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand for a hint.
+//
+// Make me compile only by reordering the lines in `main()`, but without 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
diff --git a/exercises/move_semantics/move_semantics6.rs b/exercises/move_semantics/move_semantics6.rs
index eb52a84..cace4ca 100644
--- a/exercises/move_semantics/move_semantics6.rs
+++ b/exercises/move_semantics/move_semantics6.rs
@@ -1,6 +1,9 @@
// move_semantics6.rs
-// Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand for a hint.
+//
// You can't change anything except adding or removing references.
+//
+// Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand
+// for a hint.
// I AM NOT DONE
diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs
index 1f891b0..e131b48 100644
--- a/exercises/options/options1.rs
+++ b/exercises/options/options1.rs
@@ -1,5 +1,7 @@
// options1.rs
-// Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint options1` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
@@ -7,8 +9,9 @@
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
// all, so there'll be no more left :(
fn maybe_icecream(time_of_day: u16) -> Option {
- // We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a value of 0
- // The Option output should gracefully handle cases where time_of_day > 23.
+ // We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a
+ // value of 0 The Option output should gracefully handle cases where
+ // time_of_day > 23.
// TODO: Complete the function body - remember to return an Option!
???
}
@@ -28,7 +31,8 @@ mod tests {
#[test]
fn raw_value() {
- // TODO: Fix this test. How do you get at the value contained in the Option?
+ // TODO: Fix this test. How do you get at the value contained in the
+ // Option?
let icecreams = maybe_icecream(12);
assert_eq!(icecreams, 5);
}
diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs
index 4e36443..4d998e7 100644
--- a/exercises/options/options2.rs
+++ b/exercises/options/options2.rs
@@ -1,5 +1,7 @@
// options2.rs
-// Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint.
+//
+// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
+// hint.
// I AM NOT DONE
@@ -18,17 +20,23 @@ mod tests {
#[test]
fn layered_option() {
- let mut range = 10;
- let mut optional_integers: Vec