Compare commits

..

9 commits

Author SHA1 Message Date
mo8it 0f4cb94cfe quiz2: Use repeat 2024-07-07 20:28:31 +02:00
Mo 6469e9734b
Merge pull request #2031 from NewtonChutney/patch-1
Update iterator solution in quiz2.rs
2024-07-07 20:25:21 +02:00
NitinKM 5372caefb3
Update iterator sol in quiz2.rs 2024-07-07 23:19:38 +05:30
mo8it 9d7b973a62 Improve the comments in cow1 2024-07-07 17:03:00 +02:00
mo8it a5f221aa39 Improve some messages 2024-07-07 15:53:48 +02:00
mo8it e764b75aef This'll -> This will 2024-07-07 15:41:35 +02:00
mo8it 708cfef3f7 enums3: Avoid confusion with parentheses 2024-07-07 15:29:05 +02:00
mo8it 01b8432d58 Mark the last exercise as done 2024-07-07 13:55:39 +02:00
mo8it 9b5b652c71 Fix link on website 2024-07-07 00:28:17 +02:00
10 changed files with 36 additions and 40 deletions

View file

@ -19,7 +19,7 @@ It contains code examples and exercises similar to Rustlings, but online.
Before installing Rustlings, you need to have _Rust installed_.
Visit [www.rust-lang.org/tools/install](https://www.rust-lang.org/tools/install) for further instructions on installing Rust.
This'll also install _Cargo_, Rust's package/project manager.
This will also install _Cargo_, Rust's package/project manager.
> 🐧 If you're on Linux, make sure you've installed `gcc` (for a linker).
>
@ -124,7 +124,7 @@ Continue practicing your Rust skills by building your own projects, contributing
Do you want to create your own set of Rustlings exercises to focus on some specific topic?
Or do you want to translate the original Rustlings exercises?
Then follow the link to the guide about [third-party exercises](THIRD_PARTY_EXERCISES.md)!
Then follow the link to the guide about [third-party exercises](https://github.com/rust-lang/rustlings/blob/main/THIRD_PARTY_EXERCISES.md)!
## Uninstalling Rustlings

View file

@ -1 +1 @@
This file is used to check if the user tries to run Rustlings in the repository (the method before v6)
This file is used to check if the user tries to run Rustlings in the repository (the method before version 6)

View file

@ -12,6 +12,7 @@ struct State {
height: u64,
position: Point,
message: String,
// RGB color composed of red, green and blue.
color: (u8, u8, u8),
quit: bool,
}
@ -30,8 +31,8 @@ impl State {
self.message = s;
}
fn change_color(&mut self, color: (u8, u8, u8)) {
self.color = color;
fn change_color(&mut self, red: u8, green: u8, blue: u8) {
self.color = (red, green, blue);
}
fn quit(&mut self) {
@ -39,9 +40,8 @@ impl State {
}
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:
// e.g. `foo((t, u, p, l, e))`
// TODO: Create a match expression to process the different message
// variants using the methods defined above.
}
}

View file

@ -45,8 +45,9 @@ mod tests {
#[test]
fn owned_no_mutation() {
// We can also pass `vec` without `&` so `Cow` owns it directly. In this
// case, no mutation occurs and thus also no clone. But the result is
// still owned because it was never borrowed or mutated.
// case, no mutation occurs (all numbers are already absolute) and thus
// also no clone. But the result is still owned because it was never
// borrowed or mutated.
let vec = vec![0, 1, 2];
let mut input = Cow::from(vec);
abs_all(&mut input);
@ -56,9 +57,9 @@ mod tests {
#[test]
fn owned_mutation() {
// Of course this is also the case if a mutation does occur. In this
// case, the call to `to_mut()` in the `abs_all` function returns a
// reference to the same data as before.
// Of course this is also the case if a mutation does occur (not all
// numbers are absolute). In this case, the call to `to_mut()` in the
// `abs_all` function returns a reference to the same data as before.
let vec = vec![-1, 0, 1];
let mut input = Cow::from(vec);
abs_all(&mut input);

View file

@ -34,8 +34,8 @@ impl State {
self.message = s;
}
fn change_color(&mut self, color: (u8, u8, u8)) {
self.color = color;
fn change_color(&mut self, red: u8, green: u8, blue: u8) {
self.color = (red, green, blue);
}
fn quit(&mut self) {
@ -47,7 +47,7 @@ impl State {
Message::Resize { width, height } => self.resize(width, height),
Message::Move(point) => self.move_position(point),
Message::Echo(s) => self.echo(s),
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
Message::ChangeColor(r, g, b) => self.change_color(r, g, b),
Message::Quit => self.quit(),
}
}

View file

@ -45,8 +45,9 @@ mod tests {
#[test]
fn owned_no_mutation() {
// We can also pass `vec` without `&` so `Cow` owns it directly. In this
// case, no mutation occurs and thus also no clone. But the result is
// still owned because it was never borrowed or mutated.
// case, no mutation occurs (all numbers are already absolute) and thus
// also no clone. But the result is still owned because it was never
// borrowed or mutated.
let vec = vec![0, 1, 2];
let mut input = Cow::from(vec);
abs_all(&mut input);
@ -56,9 +57,9 @@ mod tests {
#[test]
fn owned_mutation() {
// Of course this is also the case if a mutation does occur. In this
// case, the call to `to_mut()` in the `abs_all` function returns a
// reference to the same data as before.
// Of course this is also the case if a mutation does occur (not all
// numbers are absolute). In this case, the call to `to_mut()` in the
// `abs_all` function returns a reference to the same data as before.
let vec = vec![-1, 0, 1];
let mut input = Cow::from(vec);
abs_all(&mut input);

View file

@ -24,17 +24,12 @@ mod my_module {
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
let mut output = Vec::new();
for (mut string, command) in input {
for (string, command) in input {
// Create the new string.
let new_string = match command {
Command::Uppercase => string.to_uppercase(),
Command::Trim => string.trim().to_string(),
Command::Append(n) => {
for _ in 0..n {
string += "bar";
}
string
}
Command::Append(n) => string + &"bar".repeat(n),
};
// Push the new string to the output vector.
@ -49,15 +44,10 @@ mod my_module {
pub fn transformer_iter(input: Vec<(String, Command)>) -> Vec<String> {
input
.into_iter()
.map(|(mut string, command)| match command {
.map(|(string, command)| match command {
Command::Uppercase => string.to_uppercase(),
Command::Trim => string.trim().to_string(),
Command::Append(n) => {
for _ in 0..n {
string += "bar";
}
string
}
Command::Append(n) => string + &"bar".repeat(n),
})
.collect()
}

View file

@ -403,6 +403,9 @@ impl AppState {
writeln!(writer, "{}", "ok".green())?;
}
// Write that the last exercise is done.
self.write()?;
clear_terminal(writer)?;
writer.write_all(FENISH_LINE.as_bytes())?;

View file

@ -139,7 +139,7 @@ const README: &str = "# Rustlings 🦀
Welcome to these third-party Rustlings exercises 😃
First, [install Rustlings using the official instructions in the README of the Rustlings project](https://github.com/rust-lang/rustlings) ✅
First, [install Rustlings using the official instructions](https://github.com/rust-lang/rustlings) ✅
Then, open your terminal in this directory and run `rustlings` to get started with the exercises 🚀
Then, clone this repository, open a terminal in this directory and run `rustlings` to get started with the exercises 🚀
";

View file

@ -197,9 +197,10 @@ fn main() -> Result<()> {
Ok(())
}
const OLD_METHOD_ERR: &str = "You are trying to run Rustlings using the old method before v6.
const OLD_METHOD_ERR: &str =
"You are trying to run Rustlings using the old method before version 6.
The new method doesn't include cloning the Rustlings' repository.
Please follow the instructions in the README:
Please follow the instructions in `README.md`:
https://github.com/rust-lang/rustlings#getting-started";
const FORMAT_VERSION_HIGHER_ERR: &str =
@ -216,5 +217,5 @@ const PRE_INIT_MSG: &str = r"
|_| \__,_|___/\__|_|_|_| |_|\__, |___/
|___/
The `exercises` directory wasn't found in the current directory.
The `exercises/` directory couldn't be found in the current directory.
If you are just starting with Rustlings, run the command `rustlings init` to initialize it.";