1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2025-12-26 00:11:49 +02:00

Compare commits

...

56 Commits

Author SHA1 Message Date
mo8it
0847b3a4bf chore: Release 2024-07-10 16:51:34 +02:00
mo8it
6d2ea8dae3 Update CHANGELOG 2024-07-10 16:49:36 +02:00
mo8it
47ba4502e0 move_semantics2: Mention cloning in the hint 2024-07-10 15:55:18 +02:00
mo8it
6263cb6456 Add note about iterating over Option 2024-07-10 15:16:49 +02:00
mo8it
d7024d80ce move_semantics4: Avoid using the dereference operator 2024-07-10 13:50:39 +02:00
mo8it
59d6b852af move_semantics5: Move main to the end 2024-07-10 13:47:33 +02:00
mo8it
e512928df5 Update deps 2024-07-10 13:27:32 +02:00
Mo
a38029e3e7 Merge pull request #2037 from jaads/fix-iterators-2-hint
Updated name of variable name in hint
2024-07-10 11:33:45 +02:00
Jan Arends
b12b652a57 updated variable name in hint 2024-07-10 11:16:35 +02:00
mo8it
c793416495 Fix typo 2024-07-08 16:50:35 +02:00
mo8it
01343f187b Explain what a factorial is and link to wikipedia for more details 2024-07-08 16:29:43 +02:00
mo8it
69021e1497 Remove stable from book links 2024-07-08 16:00:12 +02:00
mo8it
08c408aae0 Add hint about string concatination with + 2024-07-08 15:20:56 +02:00
mo8it
bf698659b0 Sync comment from exercise 2024-07-08 15:20:23 +02:00
mo8it
2d5d70693a errors3: Add a comment to prevent changing the wrong line 2024-07-08 15:05:58 +02:00
mo8it
a4091ade5c iterators3: Add IntegerOverflow error variant 2024-07-08 14:40:35 +02:00
mo8it
a7a881809f Check is_terminal 2024-07-08 12:53:44 +02:00
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
mo8it
981a4778a9 Add newline between functions 2024-07-06 22:23:19 +02:00
mo8it
5d4363d58d Add comma 2024-07-06 22:22:52 +02:00
Mo
48697b8225 Merge pull request #2027 from yerke/patch-2
Fix formatting in strings4.rs
2024-07-06 22:22:27 +02:00
Mo
1652bb67d9 Merge pull request #2026 from yerke/patch-1
Fix typo in structs3.rs
2024-07-06 22:21:15 +02:00
Yerkebulan Tulibergenov
1499f681a3 Fix formatting in strings4.rs 2024-07-06 12:53:14 -07:00
Yerkebulan Tulibergenov
a21fa6ff40 Fix typo in structs3.rs 2024-07-06 12:37:55 -07:00
Mo
186dc3c1ab Merge pull request #2025 from mre/patch-1
Fix typo in `THIRD_PARTY_EXERCISES.md`
2024-07-06 17:06:03 +02:00
Matthias Endler
6b7a27d080 Fix typo in THIRD_PARTY_EXERCISES.md 2024-07-06 14:30:11 +02:00
Mo
c9017f9f7a Merge pull request #2022 from matthewjnield/main
chore: Update errors5.rs exercise to be consistent with solution
2024-07-06 13:25:03 +02:00
Matt Nield
fdada8b3d4 chore: Update errors5.rs exercise to be consistent with solution
In errors5.rs, there are two lines of a pattern matching block for which the order is reversed between the exercise file and the solution file. Since these lines are not changed as part of the exercise, this commit updates the exercise to make the order of the lines consistent with the solution, so that users will focus only on the lines that change between the exercise and the solution.
2024-07-05 16:04:07 -04:00
Mo
9e2ff7d037 Merge pull request #2021 from matthewjnield/main
fix: Add clarification to instructions for hashmaps2.rs
2024-07-05 15:52:20 +02:00
mo8it
65834fc420 Improve quizes 2024-07-05 15:38:59 +02:00
mo8it
f5a4965de7 Improve wording in quiz1 2024-07-05 15:38:33 +02:00
mo8it
db5911eb73 Use *= 2024-07-05 15:31:39 +02:00
Matt Nield
2f4e63b443 fix: Add clarification to instructions for hashmaps2.rs
In the instructions for hashmaps2.rs, the last sentence of the include the phrase "these fruits", which refers to fruits that were mentioned two sentences prior.

Having a sentence in between the first sentence in which the fruits were described and a later sentence in which the phrase "these fruits" is used makes this confusing to read, since the phrase "these fruits" does not come immediately after the mention of the fruits that the phrase refers to.

This pull request expands the last sentence to explicitly refer to the fruits being mentioned, in order to add clarity about the requirement of the exercise.
2024-07-05 08:27:51 -04:00
mo8it
584164a6ff Adjust enums exercises 2024-07-05 14:11:03 +02:00
mo8it
e6f6d26d13 Import enum variants in all tests 2024-07-05 13:45:14 +02:00
Mo
67d8d5848c Merge pull request #1774 from matthri/fix-enum-variant-inconsistency
Make enum variants more consistent between exercises
2024-07-05 13:43:25 +02:00
mo8it
43d15f09f0 Readd "structs" 2024-07-05 13:41:04 +02:00
mo8it
7123c7ae3a Merge remote-tracking branch 'upstream/main' into fix-enum-variant-inconsistency 2024-07-05 13:39:50 +02:00
Mo
4d9c346a17 Merge pull request #2019 from Nahor/iterator5
Add alternative solution for iterators5
2024-07-05 11:45:42 +02:00
Nahor
deed9d3943 Add alternative solution for iterators5 2024-07-04 16:35:39 -07:00
mo8it
652f0c7676 Fix tests 2024-07-04 23:39:06 +02:00
Mo
e479ec8fb6 Merge pull request #2018 from Nahor/iterators4
Unify fn signature in iterators4 exercise and solution
2024-07-04 23:37:38 +02:00
Nahor
a33501e6a7 Unify fn signature in iterators4 exercise and solution
Since this is not about conversion, prefer the option that doesn't
require one.
2024-07-04 14:23:34 -07:00
Mo
47f8199a99 Merge pull request #2017 from Nahor/main
Fix misleading test name
2024-07-04 21:14:02 +02:00
mo8it
4bf0ddc0e1 Check exercises unsolved 2024-07-04 21:12:57 +02:00
Nahor
4cd0ccce83 Fix misleading test name 2024-07-04 11:58:09 -07:00
mo8it
a3657188b6 Check for missing TODO comments 2024-07-04 20:28:46 +02:00
Matthias Richter
77b687d501 fix(enums) make enum variants more consistent
closes #1545
2023-11-14 08:19:40 +01:00
44 changed files with 393 additions and 231 deletions

View File

@@ -1,3 +1,22 @@
<a name="6.1.0"></a>
## 6.1.0 (2024-07-10)
#### Added
- `dev check`: Check that all exercises (including third-party ones) include at least one `TODO` comment.
- `dev check`: Check that all exercises actually fail to run (not already solved).
#### Changed
- Make enum variants more consistent between enum exercises.
- `iterators3`: Teach about the possible case of integer overflow during division.
#### Fixed
- Exit with a helpful error message on missing/unsupported terminal/TTY.
- Mark the last exercise as done.
<a name="6.0.1"></a>
## 6.0.1 (2024-07-04)
@@ -64,7 +83,7 @@ This should avoid issues related to the language server or to running exercises,
Clippy lints are now shown on all exercises, not only the Clippy exercises 📎
Make Clippy your friend from early on 🥰
### Third party exercises
### Third-party exercises
Rustlings now supports third-party exercises!

50
Cargo.lock generated
View File

@@ -151,9 +151,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.8"
version = "4.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84b3edb18336f4df585bc9aa31dd99c036dfa5dc5e9a2939a722a188f3a8970d"
checksum = "64acc1846d54c1fe936a78dc189c34e28d3f5afc348403f28ecf53660b9b8462"
dependencies = [
"clap_builder",
"clap_derive",
@@ -161,9 +161,9 @@ dependencies = [
[[package]]
name = "clap_builder"
version = "4.5.8"
version = "4.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1c09dd5ada6c6c78075d6fd0da3f90d8080651e2d6cc8eb2f1aaa4034ced708"
checksum = "6fb8393d67ba2e7bfaf28a23458e4e2b543cc73a99595511eb207fdb8aede942"
dependencies = [
"anstream",
"anstyle",
@@ -354,15 +354,6 @@ version = "1.70.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
[[package]]
name = "itertools"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.13.0"
@@ -594,7 +585,7 @@ dependencies = [
"cassowary",
"compact_str",
"crossterm",
"itertools 0.13.0",
"itertools",
"lru",
"paste",
"stability",
@@ -654,7 +645,7 @@ checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
[[package]]
name = "rustlings"
version = "6.0.1"
version = "6.1.0"
dependencies = [
"anyhow",
"assert_cmd",
@@ -673,7 +664,7 @@ dependencies = [
[[package]]
name = "rustlings-macros"
version = "6.0.1"
version = "6.1.0"
dependencies = [
"quote",
"serde",
@@ -709,18 +700,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.203"
version = "1.0.204"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094"
checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.203"
version = "1.0.204"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222"
dependencies = [
"proc-macro2",
"quote",
@@ -785,9 +776,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "stability"
version = "0.2.0"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ff9eaf853dec4c8802325d8b6d3dffa86cc707fd7a1a4cdbf416e13b061787a"
checksum = "d904e7009df136af5297832a3ace3370cd14ff1546a232f4f185036c2736fcac"
dependencies = [
"quote",
"syn",
@@ -829,9 +820,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.68"
version = "2.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9"
checksum = "2f0209b68b3613b093e0ec905354eccaedcfe83b8cb37cbdeae64026c3064c16"
dependencies = [
"proc-macro2",
"quote",
@@ -855,9 +846,9 @@ dependencies = [
[[package]]
name = "toml_edit"
version = "0.22.14"
version = "0.22.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38"
checksum = "d59a3a72298453f564e2b111fa896f8d07fabb36f51f06d7e875fc5e0b5a3ef1"
dependencies = [
"indexmap",
"serde",
@@ -880,11 +871,12 @@ checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202"
[[package]]
name = "unicode-truncate"
version = "1.0.0"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5fbabedabe362c618c714dbefda9927b5afc8e2a8102f47f081089a9019226"
checksum = "b3644627a5af5fa321c95b9b235a72fd24cd29c648c2c379431e6628655627bf"
dependencies = [
"itertools 0.12.1",
"itertools",
"unicode-segmentation",
"unicode-width",
]

View File

@@ -8,7 +8,7 @@ exclude = [
]
[workspace.package]
version = "6.0.1"
version = "6.1.0"
authors = [
"Liv <mokou@fastmail.com>",
"Mo Bitar <mo8it@proton.me>",
@@ -20,8 +20,8 @@ license = "MIT"
edition = "2021"
[workspace.dependencies]
serde = { version = "1.0.203", features = ["derive"] }
toml_edit = { version = "0.22.14", default-features = false, features = ["parse", "serde"] }
serde = { version = "1.0.204", features = ["derive"] }
toml_edit = { version = "0.22.15", default-features = false, features = ["parse", "serde"] }
[package]
name = "rustlings"
@@ -47,13 +47,13 @@ include = [
[dependencies]
anyhow = "1.0.86"
clap = { version = "4.5.8", features = ["derive"] }
clap = { version = "4.5.9", features = ["derive"] }
crossterm = "0.27.0"
hashbrown = "0.14.5"
notify-debouncer-mini = { version = "0.4.1", default-features = false }
os_pipe = "1.2.0"
ratatui = { version = "0.27.0", default-features = false, features = ["crossterm"] }
rustlings-macros = { path = "rustlings-macros", version = "=6.0.1" }
rustlings-macros = { path = "rustlings-macros", version = "=6.1.0" }
serde_json = "1.0.120"
serde.workspace = true
toml_edit.workspace = true

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,7 +1,7 @@
# Third-Party Exercises
The support of Rustlings for third-party exercises allows you to create your own set of Rustlings exercises to focus on some specific topic.
You could also offer a translatation of the original Rustlings exercises as third-party exercises.
You could also offer a translation of the original Rustlings exercises as third-party exercises.
## Getting started

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

@@ -5,5 +5,5 @@ compiler. In this section, we'll go through the most important ones.
## Further information
- [Data Types](https://doc.rust-lang.org/stable/book/ch03-02-data-types.html)
- [The Slice Type](https://doc.rust-lang.org/stable/book/ch04-03-slices.html)
- [Data Types](https://doc.rust-lang.org/book/ch03-02-data-types.html)
- [The Slice Type](https://doc.rust-lang.org/book/ch04-03-slices.html)

View File

@@ -12,6 +12,6 @@ the other useful data structure, hash maps, later.
## Further information
- [Storing Lists of Values with Vectors](https://doc.rust-lang.org/stable/book/ch08-01-vectors.html)
- [Storing Lists of Values with Vectors](https://doc.rust-lang.org/book/ch08-01-vectors.html)
- [`iter_mut`](https://doc.rust-lang.org/std/primitive.slice.html#method.iter_mut)
- [`map`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map)

View File

@@ -7,12 +7,12 @@ mod tests {
// TODO: Fix the compiler errors only by reordering the lines in the test.
// Don't add, change or remove any line.
#[test]
fn move_semantics5() {
let mut x = 100;
fn move_semantics4() {
let mut x = Vec::new();
let y = &mut x;
let z = &mut x;
*y += 100;
*z += 1000;
assert_eq!(x, 1200);
y.push(42);
z.push(13);
assert_eq!(x, [42, 13]);
}
}

View File

@@ -3,14 +3,6 @@
// TODO: Fix the compiler errors without changing anything except adding or
// removing references (the character `&`).
fn main() {
let data = "Rust is great!".to_string();
get_char(data);
string_uppercase(&data);
}
// Shouldn't take ownership
fn get_char(data: String) -> char {
data.chars().last().unwrap()
@@ -22,3 +14,11 @@ fn string_uppercase(mut data: &String) {
println!("{data}");
}
fn main() {
let data = "Rust is great!".to_string();
get_char(data);
string_uppercase(&data);
}

View File

@@ -1,5 +1,5 @@
// Structs contain data, but can also have logic. In this exercise we have
// defined the `Package` struct and we want to test some logic attached to it.
// Structs contain data, but can also have logic. In this exercise, we have
// defined the `Package` struct, and we want to test some logic attached to it.
#[derive(Debug)]
struct Package {

View File

@@ -4,8 +4,9 @@ enum Message {
}
fn main() {
println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo);
println!("{:?}", Message::Resize);
println!("{:?}", Message::Move);
println!("{:?}", Message::Echo);
println!("{:?}", Message::ChangeColor);
println!("{:?}", Message::Quit);
}

View File

@@ -1,4 +1,11 @@
#[allow(dead_code)]
#![allow(dead_code)]
#[derive(Debug)]
struct Point {
x: u64,
y: u64,
}
#[derive(Debug)]
enum Message {
// TODO: Define the different variants used below.
@@ -12,7 +19,11 @@ impl Message {
fn main() {
let messages = [
Message::Move { x: 10, y: 30 },
Message::Resize {
width: 10,
height: 30,
},
Message::Move(Point { x: 10, y: 15 }),
Message::Echo(String::from("hello world")),
Message::ChangeColor(200, 255, 255),
Message::Quit,

View File

@@ -1,40 +1,47 @@
struct Point {
x: u64,
y: u64,
}
enum Message {
// TODO: Implement the message variant types based on their usage below.
}
struct Point {
x: u8,
y: u8,
}
struct State {
color: (u8, u8, u8),
width: u64,
height: u64,
position: Point,
quit: bool,
message: String,
// RGB color composed of red, green and blue.
color: (u8, u8, u8),
quit: bool,
}
impl State {
fn change_color(&mut self, color: (u8, u8, u8)) {
self.color = color;
}
fn quit(&mut self) {
self.quit = true;
}
fn echo(&mut self, s: String) {
self.message = s;
fn resize(&mut self, width: u64, height: u64) {
self.width = width;
self.height = height;
}
fn move_position(&mut self, point: Point) {
self.position = point;
}
fn echo(&mut self, s: String) {
self.message = s;
}
fn change_color(&mut self, red: u8, green: u8, blue: u8) {
self.color = (red, green, blue);
}
fn quit(&mut self) {
self.quit = true;
}
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.
}
}
@@ -49,21 +56,29 @@ mod tests {
#[test]
fn test_match_message_call() {
let mut state = State {
quit: false,
width: 0,
height: 0,
position: Point { x: 0, y: 0 },
color: (0, 0, 0),
message: String::from("hello world"),
color: (0, 0, 0),
quit: false,
};
state.process(Message::ChangeColor(255, 0, 255));
state.process(Message::Echo(String::from("Hello world!")));
state.process(Message::Resize {
width: 10,
height: 30,
});
state.process(Message::Move(Point { x: 10, y: 15 }));
state.process(Message::Echo(String::from("Hello world!")));
state.process(Message::ChangeColor(255, 0, 255));
state.process(Message::Quit);
assert_eq!(state.color, (255, 0, 255));
assert_eq!(state.width, 10);
assert_eq!(state.height, 30);
assert_eq!(state.position.x, 10);
assert_eq!(state.position.y, 15);
assert!(state.quit);
assert_eq!(state.message, "Hello world!");
assert_eq!(state.color, (255, 0, 255));
assert!(state.quit);
}
}

View File

@@ -4,6 +4,7 @@ fn placeholder() {}
fn string_slice(arg: &str) {
println!("{arg}");
}
fn string(arg: String) {
println!("{arg}");
}

View File

@@ -5,7 +5,8 @@
// 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!
// to insert any more of the fruits that are already in the basket (Apple,
// Mango, and Lyche).
use std::collections::HashMap;

View File

@@ -14,7 +14,7 @@ Option types are very common in Rust code, as they have a number of uses:
## Further Information
- [Option Enum Format](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-enum-definitions)
- [Option Enum Format](https://doc.rust-lang.org/book/ch10-01-syntax.html#in-enum-definitions)
- [Option Module Documentation](https://doc.rust-lang.org/std/option/)
- [Option Enum Documentation](https://doc.rust-lang.org/std/option/enum.Option.html)
- [if let](https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html)

View File

@@ -19,6 +19,7 @@ fn main() {
let mut tokens = 100;
let pretend_user_input = "8";
// Don't change this line.
let cost = total_cost(pretend_user_input)?;
if cost > tokens {

View File

@@ -39,8 +39,8 @@ struct PositiveNonzeroInteger(u64);
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
match value {
0 => Err(CreationError::Zero),
x if x < 0 => Err(CreationError::Negative),
0 => Err(CreationError::Zero),
x => Ok(PositiveNonzeroInteger(x as u64)),
}
}

View File

@@ -7,5 +7,5 @@ The simplest and most common use of generics is for type parameters.
## Further information
- [Generic Data Types](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html)
- [Generic Data Types](https://doc.rust-lang.org/book/ch10-01-syntax.html)
- [Bounds](https://doc.rust-lang.org/rust-by-example/generics/bounds.html)

View File

@@ -1,12 +1,16 @@
#[derive(Debug, PartialEq, Eq)]
enum DivisionError {
// Example: 42 / 0
DivideByZero,
// Only case for `i64`: `i64::MIN / -1` because the result is `i64::MAX + 1`
IntegerOverflow,
// Example: 5 / 2 = 2.5
NotDivisible,
}
// TODO: Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
// Otherwise, return a suitable error.
fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
todo!();
}
@@ -42,6 +46,11 @@ mod tests {
assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
}
#[test]
fn test_integer_overflow() {
assert_eq!(divide(i64::MIN, -1), Err(DivisionError::IntegerOverflow));
}
#[test]
fn test_not_divisible() {
assert_eq!(divide(81, 6), Err(DivisionError::NotDivisible));

View File

@@ -1,5 +1,8 @@
fn factorial(num: u8) -> u64 {
// TODO: Complete this function to return the factorial of `num`.
fn factorial(num: u64) -> u64 {
// TODO: Complete this function to return the factorial of `num` which is
// defined as `1 * 2 * 3 * … * num`.
// https://en.wikipedia.org/wiki/Factorial
//
// Do not use:
// - early returns (using the `return` keyword explicitly)
// Try not to use:

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

@@ -5,12 +5,12 @@
//
// Mary is buying apples. The price of an apple is calculated as follows:
// - An apple costs 2 rustbucks.
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
// - However, if Mary buys more than 40 apples, the price of each apple in the
// entire order is reduced to only 1 rustbuck!
// TODO: Write a function that calculates the price of an order of apples given
// the quantity bought.
// Put your function here!
// fn calculate_price_of_apples(???) -> ??? {
// fn calculate_price_of_apples(???) -> ??? { ??? }
fn main() {
// You can optionally experiment here.

View File

@@ -31,6 +31,7 @@ https://github.com/rust-lang/rustlings/blob/main/CONTRIBUTING.md"""
name = "intro1"
dir = "00_intro"
test = false
skip_check_unsolved = true
hint = """
Enter `n` to move on to the next exercise.
You might need to press ENTER after typing `n`."""
@@ -308,7 +309,7 @@ In Rust, there are two ways to define a Vector.
inside the square brackets. This way is simpler when you exactly know
the initial values.
Check this chapter: https://doc.rust-lang.org/stable/book/ch08-01-vectors.html
Check this chapter: https://doc.rust-lang.org/book/ch08-01-vectors.html
of the Rust book to learn more."""
[[exercises]]
@@ -350,7 +351,7 @@ We call this "moving" a variable. When we pass `vec0` into `fill_vec`, it's
being "moved" into `vec1`, meaning we can't access `vec0` anymore.
You could make another, separate version of the data that's in `vec0` and
pass it to `fill_vec` instead."""
pass it to `fill_vec` instead. This is called cloning in Rust."""
[[exercises]]
name = "move_semantics3"
@@ -377,7 +378,7 @@ dir = "06_move_semantics"
test = false
hint = """
To find the answer, you can consult the book section "References and Borrowing":
https://doc.rust-lang.org/stable/book/ch04-02-references-and-borrowing.html
https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html
The first problem is that `get_char` is taking ownership of the string. So
`data` is moved and can't be used for `string_uppercase`. `data` is moved to
@@ -415,7 +416,7 @@ to its fields.
There are however some shortcuts that can be taken when instantiating structs.
Have a look in The Book to find out more:
https://doc.rust-lang.org/stable/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax"""
https://doc.rust-lang.org/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax"""
[[exercises]]
name = "structs3"
@@ -444,7 +445,7 @@ dir = "08_enums"
test = false
hint = """
You can create enumerations that have different variants with different types
such as no data, anonymous structs, a single string, tuples, etc."""
such as anonymous structs, structs, a single string, tuples, no data, etc."""
[[exercises]]
name = "enums3"
@@ -486,7 +487,7 @@ to add one character to the `if` statement, though, that will coerce the
Side note: If you're interested in learning about how this kind of reference
conversion works, you can jump ahead in the book and read this part in the
smart pointers chapter:
https://doc.rust-lang.org/stable/book/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods"""
https://doc.rust-lang.org/book/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods"""
[[exercises]]
name = "strings3"
@@ -560,7 +561,7 @@ hint = """
Use the `entry()` and `or_insert()` methods of `HashMap` to achieve this.
Learn more in The Book:
https://doc.rust-lang.org/stable/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value"""
https://doc.rust-lang.org/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value"""
[[exercises]]
name = "hashmaps3"
@@ -571,7 +572,7 @@ Hint 1: Use the `entry()` and `or_insert()` (or `or_insert_with()`) methods of
exist in the table yet.
Learn more in The Book:
https://doc.rust-lang.org/stable/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value
https://doc.rust-lang.org/book/ch08-03-hash-maps.html#only-inserting-a-value-if-the-key-has-no-value
Hint 2: If there is already an entry for a given key, the value returned by
`entry()` can be updated based on the existing value.
@@ -584,7 +585,7 @@ https://doc.rust-lang.org/book/ch08-03-hash-maps.html#updating-a-value-based-on-
[[exercises]]
name = "quiz2"
dir = "quizzes"
hint = "No hints this time ;)"
hint = "The `+` operator can concatenate a `String` with a `&str`."
# OPTIONS
@@ -738,7 +739,7 @@ name = "generics2"
dir = "14_generics"
hint = """
Related section in The Book:
https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-method-definitions"""
https://doc.rust-lang.org/book/ch10-01-syntax.html#in-method-definitions"""
# TRAITS
@@ -747,7 +748,9 @@ name = "traits1"
dir = "15_traits"
hint = """
More about traits in The Book:
https://doc.rust-lang.org/book/ch10-02-traits.html"""
https://doc.rust-lang.org/book/ch10-02-traits.html
The `+` operator can concatenate a `String` with a `&str`."""
[[exercises]]
name = "traits2"
@@ -868,7 +871,7 @@ We expect the method `Rectangle::new` to panic for negative values.
To handle that, you need to add a special attribute to the test function.
You can refer to the docs:
https://doc.rust-lang.org/stable/book/ch11-01-writing-tests.html#checking-for-panics-with-should_panic"""
https://doc.rust-lang.org/book/ch11-01-writing-tests.html#checking-for-panics-with-should_panic"""
# STANDARD LIBRARY TYPES
@@ -887,9 +890,9 @@ hint = """
`capitalize_first`:
The variable `first` is a `char`. It needs to be capitalized and added to the
remaining characters in `c` in order to return the correct `String`.
remaining characters in `chars` in order to return the correct `String`.
The remaining characters in `c` can be viewed as a string slice using the
The remaining characters in `chars` can be viewed as a string slice using the
`as_str` method.
The documentation for `char` contains many useful methods.
@@ -1004,7 +1007,7 @@ thread-local copy of the numbers.
This is a simple exercise if you understand the underlying concepts, but if this
is too much of a struggle, consider reading through all of Chapter 16 in The
Book:
https://doc.rust-lang.org/stable/book/ch16-00-concurrency.html"""
https://doc.rust-lang.org/book/ch16-00-concurrency.html"""
[[exercises]]
name = "cow1"
@@ -1142,7 +1145,11 @@ test = false
strict_clippy = true
hint = """
`for` loops over `Option` values are more clearly expressed as an `if-let`
statement."""
statement.
Not required to solve this exercise, but if you are interested in when iterating
over `Option` can be useful, read the following section in the documentation:
https://doc.rust-lang.org/std/option/#iterating-over-option"""
[[exercises]]
name = "clippy3"

View File

@@ -7,15 +7,15 @@ mod tests {
// TODO: Fix the compiler errors only by reordering the lines in the test.
// Don't add, change or remove any line.
#[test]
fn move_semantics5() {
let mut x = 100;
fn move_semantics4() {
let mut x = Vec::new();
let y = &mut x;
// `y` used here.
*y += 100;
y.push(42);
// The mutable reference `y` is not used anymore,
// therefore a new reference can be created.
let z = &mut x;
*z += 1000;
assert_eq!(x, 1200);
z.push(13);
assert_eq!(x, [42, 13]);
}
}

View File

@@ -1,13 +1,5 @@
#![allow(clippy::ptr_arg)]
fn main() {
let data = "Rust is great!".to_string();
get_char(&data);
string_uppercase(data);
}
// Borrows instead of taking ownership.
// It is recommended to use `&str` instead of `&String` here. But this is
// enough for now because we didn't handle strings yet.
@@ -21,3 +13,11 @@ fn string_uppercase(mut data: String) {
println!("{data}");
}
fn main() {
let data = "Rust is great!".to_string();
get_char(&data);
string_uppercase(data);
}

View File

@@ -1,14 +1,16 @@
#[derive(Debug)]
enum Message {
Quit,
Echo,
Resize,
Move,
Echo,
ChangeColor,
Quit,
}
fn main() {
println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo);
println!("{:?}", Message::Resize);
println!("{:?}", Message::Move);
println!("{:?}", Message::Echo);
println!("{:?}", Message::ChangeColor);
println!("{:?}", Message::Quit);
}

View File

@@ -1,7 +1,15 @@
#[allow(dead_code)]
#![allow(dead_code)]
#[derive(Debug)]
struct Point {
x: u64,
y: u64,
}
#[derive(Debug)]
enum Message {
Move { x: i64, y: i64 },
Resize { width: u64, height: u64 },
Move(Point),
Echo(String),
ChangeColor(u8, u8, u8),
Quit,
@@ -15,7 +23,11 @@ impl Message {
fn main() {
let messages = [
Message::Move { x: 10, y: 30 },
Message::Resize {
width: 10,
height: 30,
},
Message::Move(Point { x: 10, y: 15 }),
Message::Echo(String::from("hello world")),
Message::ChangeColor(200, 255, 255),
Message::Quit,

View File

@@ -1,44 +1,53 @@
struct Point {
x: u64,
y: u64,
}
enum Message {
ChangeColor(u8, u8, u8),
Echo(String),
Resize { width: u64, height: u64 },
Move(Point),
Echo(String),
ChangeColor(u8, u8, u8),
Quit,
}
struct Point {
x: u8,
y: u8,
}
struct State {
color: (u8, u8, u8),
width: u64,
height: u64,
position: Point,
quit: bool,
message: String,
color: (u8, u8, u8),
quit: bool,
}
impl State {
fn change_color(&mut self, color: (u8, u8, u8)) {
self.color = color;
}
fn quit(&mut self) {
self.quit = true;
}
fn echo(&mut self, s: String) {
self.message = s;
fn resize(&mut self, width: u64, height: u64) {
self.width = width;
self.height = height;
}
fn move_position(&mut self, point: Point) {
self.position = point;
}
fn echo(&mut self, s: String) {
self.message = s;
}
fn change_color(&mut self, red: u8, green: u8, blue: u8) {
self.color = (red, green, blue);
}
fn quit(&mut self) {
self.quit = true;
}
fn process(&mut self, message: Message) {
match message {
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
Message::Echo(s) => self.echo(s),
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::Quit => self.quit(),
}
}
@@ -55,21 +64,29 @@ mod tests {
#[test]
fn test_match_message_call() {
let mut state = State {
quit: false,
width: 0,
height: 0,
position: Point { x: 0, y: 0 },
color: (0, 0, 0),
message: String::from("hello world"),
color: (0, 0, 0),
quit: false,
};
state.process(Message::ChangeColor(255, 0, 255));
state.process(Message::Echo(String::from("Hello world!")));
state.process(Message::Resize {
width: 10,
height: 30,
});
state.process(Message::Move(Point { x: 10, y: 15 }));
state.process(Message::Echo(String::from("Hello world!")));
state.process(Message::ChangeColor(255, 0, 255));
state.process(Message::Quit);
assert_eq!(state.color, (255, 0, 255));
assert_eq!(state.width, 10);
assert_eq!(state.height, 30);
assert_eq!(state.position.x, 10);
assert_eq!(state.position.y, 15);
assert!(state.quit);
assert_eq!(state.message, "Hello world!");
assert_eq!(state.color, (255, 0, 255));
assert!(state.quit);
}
}

View File

@@ -1,6 +1,7 @@
fn string_slice(arg: &str) {
println!("{arg}");
}
fn string(arg: String) {
println!("{arg}");
}

View File

@@ -1,6 +1,10 @@
#[derive(Debug, PartialEq, Eq)]
enum DivisionError {
// Example: 42 / 0
DivideByZero,
// Only case for `i64`: `i64::MIN / -1` because the result is `i64::MAX + 1`
IntegerOverflow,
// Example: 5 / 2 = 2.5
NotDivisible,
}
@@ -9,6 +13,10 @@ fn divide(a: i64, b: i64) -> Result<i64, DivisionError> {
return Err(DivisionError::DivideByZero);
}
if a == i64::MIN && b == -1 {
return Err(DivisionError::IntegerOverflow);
}
if a % b != 0 {
return Err(DivisionError::NotDivisible);
}
@@ -51,6 +59,11 @@ mod tests {
assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
}
#[test]
fn test_integer_overflow() {
assert_eq!(divide(i64::MIN, -1), Err(DivisionError::IntegerOverflow));
}
#[test]
fn test_not_divisible() {
assert_eq!(divide(81, 6), Err(DivisionError::NotDivisible));

View File

@@ -47,6 +47,23 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
.sum()
}
// Equivalent to `count_collection_iterator` and `count_iterator`, iterating as
// if the collection was a single container instead of a container of containers
// (and more accurately, a single iterator instead of an iterator of iterators).
fn count_collection_iterator_flat(
collection: &[HashMap<String, Progress>],
value: Progress,
) -> usize {
// `collection` is a slice of hash maps.
// collection = [{ "variables1": Complete, "from_str": None, … },
// { "variables2": Complete, … }, … ]
collection
.iter()
.flat_map(HashMap::values) // or just `.flatten()` when wanting the default iterator (`HashMap::iter`)
.filter(|val| **val == value)
.count()
}
fn main() {
// You can optionally experiment here.
}
@@ -54,10 +71,9 @@ fn main() {
#[cfg(test)]
mod tests {
use super::*;
use Progress::*;
fn get_map() -> HashMap<String, Progress> {
use Progress::*;
let mut map = HashMap::new();
map.insert(String::from("variables1"), Complete);
map.insert(String::from("functions1"), Complete);
@@ -70,8 +86,6 @@ mod tests {
}
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
use Progress::*;
let map = get_map();
let mut other = HashMap::new();
@@ -87,25 +101,25 @@ mod tests {
#[test]
fn count_complete() {
let map = get_map();
assert_eq!(count_iterator(&map, Progress::Complete), 3);
assert_eq!(count_iterator(&map, Complete), 3);
}
#[test]
fn count_some() {
let map = get_map();
assert_eq!(count_iterator(&map, Progress::Some), 1);
assert_eq!(count_iterator(&map, Some), 1);
}
#[test]
fn count_none() {
let map = get_map();
assert_eq!(count_iterator(&map, Progress::None), 2);
assert_eq!(count_iterator(&map, None), 2);
}
#[test]
fn count_complete_equals_for() {
let map = get_map();
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
let progress_states = [Complete, Some, None];
for progress_state in progress_states {
assert_eq!(
count_for(&map, progress_state),
@@ -117,34 +131,38 @@ mod tests {
#[test]
fn count_collection_complete() {
let collection = get_vec_map();
assert_eq!(
count_collection_iterator(&collection, Progress::Complete),
6,
);
assert_eq!(count_collection_iterator(&collection, Complete), 6);
assert_eq!(count_collection_iterator_flat(&collection, Complete), 6);
}
#[test]
fn count_collection_some() {
let collection = get_vec_map();
assert_eq!(count_collection_iterator(&collection, Progress::Some), 1);
assert_eq!(count_collection_iterator(&collection, Some), 1);
assert_eq!(count_collection_iterator_flat(&collection, Some), 1);
}
#[test]
fn count_collection_none() {
let collection = get_vec_map();
assert_eq!(count_collection_iterator(&collection, Progress::None), 4);
assert_eq!(count_collection_iterator(&collection, None), 4);
assert_eq!(count_collection_iterator_flat(&collection, None), 4);
}
#[test]
fn count_collection_equals_for() {
let collection = get_vec_map();
let progress_states = [Progress::Complete, Progress::Some, Progress::None];
let progress_states = [Complete, Some, None];
for progress_state in progress_states {
assert_eq!(
count_collection_for(&collection, progress_state),
count_collection_iterator(&collection, progress_state),
);
assert_eq!(
count_collection_for(&collection, progress_state),
count_collection_iterator_flat(&collection, progress_state),
);
}
}
}

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

@@ -15,7 +15,7 @@ fn char_counter<T: AsRef<str>>(arg: T) -> usize {
// Squares a number using `as_mut()`.
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
let arg = arg.as_mut();
*arg = *arg * *arg;
*arg *= *arg;
}
fn main() {

View File

@@ -1,8 +1,7 @@
// Mary is buying apples. The price of an apple is calculated as follows:
// - An apple costs 2 rustbucks.
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
// Write a function that calculates the price of an order of apples given the
// quantity bought.
// - However, if Mary buys more than 40 apples, the price of each apple in the
// entire order is reduced to only 1 rustbuck!
fn calculate_price_of_apples(n_apples: u64) -> u64 {
if n_apples > 40 {

View File

@@ -1,10 +1,3 @@
// This is a quiz for the following sections:
// - Strings
// - Vecs
// - Move semantics
// - Modules
// - Enums
//
// Let's build a little machine in the form of a function. As input, we're going
// to give a list of strings and commands. These commands determine what action
// is going to be applied to the string. It can either be:
@@ -13,7 +6,7 @@
// - Append "bar" to the string a specified amount of times
//
// The exact form of this will be:
// - The input is going to be a vector of a 2-length tuple,
// - The input is going to be a vector of 2-length tuples,
// the first element is the string, the second one is the command.
// - The output element is going to be a vector of strings.
@@ -31,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.
@@ -56,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

@@ -1,7 +1,3 @@
// This quiz tests:
// - Generics
// - Traits
//
// An imaginary magical school has a new report card generation system written
// in Rust! Currently, the system only supports creating report cards where the
// student's grade is represented numerically (e.g. 1.0 -> 5.5). However, the

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

@@ -111,6 +111,7 @@ mod tests {
test: true,
strict_clippy: true,
hint: String::new(),
skip_check_unsolved: false,
},
ExerciseInfo {
name: String::from("2"),
@@ -118,6 +119,7 @@ mod tests {
test: false,
strict_clippy: false,
hint: String::new(),
skip_check_unsolved: false,
},
];

View File

@@ -92,6 +92,10 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<hashbrown::HashSet<
bail!("The `main` function is missing in the file `{path}`.\nCreate at least an empty `main` function to avoid language server errors");
}
if !file_buf.contains("// TODO") {
bail!("Didn't find any `// TODO` comment in the file `{path}`.\nYou need to have at least one such comment to guide the user.");
}
if !exercise_info.test && file_buf.contains("#[test]") {
bail!("The file `{path}` contains tests annotated with `#[test]` but the exercise `{name}` has `test = false` in the `info.toml` file");
}
@@ -158,7 +162,46 @@ fn check_unexpected_files(
Ok(())
}
fn check_exercises(info_file: &InfoFile) -> Result<()> {
fn check_exercises_unsolved(info_file: &InfoFile, target_dir: &Path) -> Result<()> {
let error_occurred = AtomicBool::new(false);
println!(
"Running all exercises to check that they aren't already solved. This may take a while…\n",
);
thread::scope(|s| {
for exercise_info in &info_file.exercises {
if exercise_info.skip_check_unsolved {
continue;
}
s.spawn(|| {
let error = |e| {
let mut stderr = io::stderr().lock();
stderr.write_all(e).unwrap();
stderr.write_all(b"\nProblem with the exercise ").unwrap();
stderr.write_all(exercise_info.name.as_bytes()).unwrap();
stderr.write_all(SEPARATOR).unwrap();
error_occurred.store(true, atomic::Ordering::Relaxed);
};
let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
match exercise_info.run_exercise(&mut output, target_dir) {
Ok(true) => error(b"Already solved!"),
Ok(false) => (),
Err(e) => error(e.to_string().as_bytes()),
}
});
}
});
if error_occurred.load(atomic::Ordering::Relaxed) {
bail!(CHECK_EXERCISES_UNSOLVED_ERR);
}
Ok(())
}
fn check_exercises(info_file: &InfoFile, target_dir: &Path) -> Result<()> {
match info_file.format_version.cmp(&CURRENT_FORMAT_VERSION) {
Ordering::Less => bail!("`format_version` < {CURRENT_FORMAT_VERSION} (supported version)\nPlease migrate to the latest format version"),
Ordering::Greater => bail!("`format_version` > {CURRENT_FORMAT_VERSION} (supported version)\nTry updating the Rustlings program"),
@@ -168,15 +211,14 @@ fn check_exercises(info_file: &InfoFile) -> Result<()> {
let info_file_paths = check_info_file_exercises(info_file)?;
check_unexpected_files("exercises", &info_file_paths)?;
Ok(())
check_exercises_unsolved(info_file, target_dir)
}
fn check_solutions(require_solutions: bool, info_file: &InfoFile) -> Result<()> {
let target_dir = parse_target_dir()?;
fn check_solutions(require_solutions: bool, info_file: &InfoFile, target_dir: &Path) -> Result<()> {
let paths = Mutex::new(hashbrown::HashSet::with_capacity(info_file.exercises.len()));
let error_occurred = AtomicBool::new(false);
println!("Running all solutions. This may take a while...\n");
println!("Running all solutions. This may take a while\n");
thread::scope(|s| {
for exercise_info in &info_file.exercises {
s.spawn(|| {
@@ -202,7 +244,7 @@ fn check_solutions(require_solutions: bool, info_file: &InfoFile) -> Result<()>
}
let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
match exercise_info.run_solution(&mut output, &target_dir) {
match exercise_info.run_solution(&mut output, target_dir) {
Ok(true) => {
paths.lock().unwrap().insert(PathBuf::from(path));
}
@@ -238,8 +280,9 @@ pub fn check(require_solutions: bool) -> Result<()> {
check_cargo_toml(&info_file.exercises, &current_cargo_toml, b"")?;
}
check_exercises(&info_file)?;
check_solutions(require_solutions, &info_file)?;
let target_dir = parse_target_dir()?;
check_exercises(&info_file, &target_dir)?;
check_solutions(require_solutions, &info_file, &target_dir)?;
println!("\nEverything looks fine!");
@@ -248,3 +291,6 @@ pub fn check(require_solutions: bool) -> Result<()> {
const SEPARATOR: &[u8] =
b"\n========================================================================================\n";
const CHECK_EXERCISES_UNSOLVED_ERR: &str = "At least one exercise is already solved or failed to run. See the output above.
If this is an intro exercise that is intended to be already solved, add `skip_check_unsolved = true` to the exercise's metadata in the `info.toml` file.";

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

@@ -11,14 +11,17 @@ pub struct ExerciseInfo {
pub name: String,
/// Exercise's directory name inside the `exercises/` directory.
pub dir: Option<String>,
#[serde(default = "default_true")]
/// Run `cargo test` on the exercise.
#[serde(default = "default_true")]
pub test: bool,
/// Deny all Clippy warnings.
#[serde(default)]
pub strict_clippy: bool,
/// The exercise's hint to be shown to the user on request.
pub hint: String,
/// The exercise is already solved. Ignore it when checking that all exercises are unsolved.
#[serde(default)]
pub skip_check_unsolved: bool,
}
#[inline(always)]
const fn default_true() -> bool {

View File

@@ -2,7 +2,7 @@ use anyhow::{bail, Context, Result};
use app_state::StateFileStatus;
use clap::{Parser, Subcommand};
use std::{
io::{self, BufRead, StdoutLock, Write},
io::{self, BufRead, IsTerminal, StdoutLock, Write},
path::Path,
process::exit,
};
@@ -148,6 +148,10 @@ fn main() -> Result<()> {
match args.command {
None => {
if !io::stdout().is_terminal() {
bail!("Unsupported or missing terminal/TTY");
}
let notify_exercise_names = if args.manual_run {
None
} else {
@@ -197,9 +201,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 +221,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.";