Skip to content

Commit

Permalink
Merge pull request #1955 from rust-lang/solutions
Browse files Browse the repository at this point in the history
Solutions
  • Loading branch information
mo8it committed Apr 24, 2024
2 parents 86684b7 + 8a085a0 commit 53fdb90
Show file tree
Hide file tree
Showing 115 changed files with 359 additions and 213 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Expand Up @@ -17,6 +17,10 @@ authors = [
license = "MIT"
edition = "2021"

[workspace.dependencies]
serde = { version = "1.0.198", features = ["derive"] }
toml_edit = { version = "0.22.12", default-features = false, features = ["parse", "serde"] }

[package]
name = "rustlings"
description = "Small exercises to get you used to reading and writing Rust code!"
Expand All @@ -41,8 +45,8 @@ hashbrown = "0.14.3"
notify-debouncer-mini = "0.4.1"
ratatui = "0.26.2"
rustlings-macros = { path = "rustlings-macros", version = "6.0.0-alpha.0" }
serde = { version = "1.0.198", features = ["derive"] }
toml_edit = { version = "0.22.12", default-features = false, features = ["parse", "serde"] }
serde.workspace = true
toml_edit.workspace = true
which = "6.0.1"

[dev-dependencies]
Expand Down
5 changes: 0 additions & 5 deletions README.md
Expand Up @@ -90,11 +90,6 @@ You can also get the hint for the next pending exercise with the following comma
rustlings hint
```

## Quizzes

After every couple of sections, there will be a quiz that'll test your knowledge on a bunch of sections at once.
These quizzes are found in `exercises/quizN.rs`.

## Continuing On

<!-- TODO: Mention third-party exercises -->
Expand Down
3 changes: 3 additions & 0 deletions exercises/quizzes/README.md
@@ -0,0 +1,3 @@
# Quizzes

After every couple of sections, there will be a quiz in this directory that'll test your knowledge on a bunch of sections at once.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions info.toml
Expand Up @@ -236,6 +236,7 @@ Make sure the type is consistent across all arms."""

[[exercises]]
name = "quiz1"
dir = "quizzes"
mode = "test"
hint = "No hints this time ;)"

Expand Down Expand Up @@ -637,6 +638,7 @@ Learn more at https://doc.rust-lang.org/book/ch08-03-hash-maps.html#updating-a-v

[[exercises]]
name = "quiz2"
dir = "quizzes"
mode = "test"
hint = "No hints this time ;)"

Expand Down Expand Up @@ -870,6 +872,7 @@ See the documentation at: https://doc.rust-lang.org/book/ch10-02-traits.html#spe

[[exercises]]
name = "quiz3"
dir = "quizzes"
mode = "test"
hint = """
To find the best solution to this challenge you're going to need to think back
Expand Down
2 changes: 2 additions & 0 deletions rustlings-macros/Cargo.toml
Expand Up @@ -11,3 +11,5 @@ proc-macro = true

[dependencies]
quote = "1.0.36"
serde.workspace = true
toml_edit.workspace = true
99 changes: 26 additions & 73 deletions rustlings-macros/src/lib.rs
@@ -1,86 +1,39 @@
use proc_macro::TokenStream;
use quote::quote;
use std::{fs::read_dir, panic, path::PathBuf};
use serde::Deserialize;

fn path_to_string(path: PathBuf) -> String {
path.into_os_string()
.into_string()
.unwrap_or_else(|original| {
panic!("The path {} is invalid UTF8", original.to_string_lossy());
})
#[derive(Deserialize)]
struct ExerciseInfo {
name: String,
dir: String,
}

#[derive(Deserialize)]
struct InfoFile {
exercises: Vec<ExerciseInfo>,
}

#[proc_macro]
pub fn include_files(_: TokenStream) -> TokenStream {
let mut files = Vec::with_capacity(8);
let mut dirs = Vec::with_capacity(128);

for entry in read_dir("exercises").expect("Failed to open the `exercises` directory") {
let entry = entry.expect("Failed to read the `exercises` directory");

if entry.file_type().unwrap().is_file() {
let path = entry.path();
if path.file_name().unwrap() != "README.md" {
files.push(path_to_string(path));
}

continue;
}

let dir_path = entry.path();
let dir_files = read_dir(&dir_path).unwrap_or_else(|e| {
panic!("Failed to open the directory {}: {e}", dir_path.display());
});
let dir_path = path_to_string(dir_path);
let dir_files = dir_files.filter_map(|entry| {
let entry = entry.unwrap_or_else(|e| {
panic!("Failed to read the directory {dir_path}: {e}");
});
let path = entry.path();

if !entry.file_type().unwrap().is_file() {
panic!("Found {} but expected only files", path.display());
}

if path.file_name().unwrap() == "README.md" {
return None;
}

Some(path_to_string(path))
});

dirs.push(quote! {
EmbeddedFlatDir {
path: #dir_path,
readme: EmbeddedFile {
path: ::std::concat!(#dir_path, "/README.md"),
content: ::std::include_bytes!(::std::concat!("../", #dir_path, "/README.md")),
},
content: &[
#(EmbeddedFile {
path: #dir_files,
content: ::std::include_bytes!(::std::concat!("../", #dir_files)),
}),*
],
}
});
}
let exercises = toml_edit::de::from_str::<InfoFile>(include_str!("../../info.toml"))
.expect("Failed to parse `info.toml`")
.exercises;

let exercise_files = exercises
.iter()
.map(|exercise| format!("../exercises/{}/{}.rs", exercise.dir, exercise.name));
let solution_files = exercises
.iter()
.map(|exercise| format!("../solutions/{}/{}.rs", exercise.dir, exercise.name));
let dirs = exercises.iter().map(|exercise| &exercise.dir);
let readmes = exercises
.iter()
.map(|exercise| format!("../exercises/{}/README.md", exercise.dir));

quote! {
EmbeddedFiles {
exercises_dir: ExercisesDir {
readme: EmbeddedFile {
path: "exercises/README.md",
content: ::std::include_bytes!("../exercises/README.md"),
},
files: &[#(
EmbeddedFile {
path: #files,
content: ::std::include_bytes!(::std::concat!("../", #files)),
}
),*],
dirs: &[#(#dirs),*],
},
exercise_files: &[#(ExerciseFiles { exercise: include_bytes!(#exercise_files), solution: include_bytes!(#solution_files) }),*],
exercise_dirs: &[#(ExerciseDir { name: #dirs, readme: include_bytes!(#readmes) }),*]
}
}
.into()
Expand Down
1 change: 1 addition & 0 deletions solutions/00_intro/intro1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/00_intro/intro2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/01_variables/variables1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/01_variables/variables2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/01_variables/variables3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/01_variables/variables4.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/01_variables/variables5.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/01_variables/variables6.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/02_functions/functions1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/02_functions/functions2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/02_functions/functions3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/02_functions/functions4.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/02_functions/functions5.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/03_if/if1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/03_if/if2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/03_if/if3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/04_primitive_types/primitive_types1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/04_primitive_types/primitive_types2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/04_primitive_types/primitive_types3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/04_primitive_types/primitive_types4.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/04_primitive_types/primitive_types5.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/04_primitive_types/primitive_types6.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/05_vecs/vecs1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/05_vecs/vecs2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/06_move_semantics/move_semantics1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/06_move_semantics/move_semantics2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/06_move_semantics/move_semantics3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/06_move_semantics/move_semantics4.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/06_move_semantics/move_semantics5.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/06_move_semantics/move_semantics6.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/07_structs/structs1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/07_structs/structs2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/07_structs/structs3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/08_enums/enums1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/08_enums/enums2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/08_enums/enums3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/09_strings/strings1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/09_strings/strings2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/09_strings/strings3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/09_strings/strings4.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/10_modules/modules1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/10_modules/modules2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/10_modules/modules3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/11_hashmaps/hashmaps1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/11_hashmaps/hashmaps2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/11_hashmaps/hashmaps3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/12_options/options1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/12_options/options2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/12_options/options3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/13_error_handling/errors1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/13_error_handling/errors2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/13_error_handling/errors3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/13_error_handling/errors4.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/13_error_handling/errors5.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/13_error_handling/errors6.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/14_generics/generics1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/14_generics/generics2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/15_traits/traits1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/15_traits/traits2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/15_traits/traits3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/15_traits/traits4.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/15_traits/traits5.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/16_lifetimes/lifetimes1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/16_lifetimes/lifetimes2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/16_lifetimes/lifetimes3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/17_tests/tests1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/17_tests/tests2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/17_tests/tests3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/17_tests/tests4.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/18_iterators/iterators1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/18_iterators/iterators2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/18_iterators/iterators3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/18_iterators/iterators4.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/18_iterators/iterators5.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/19_smart_pointers/arc1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/19_smart_pointers/box1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/19_smart_pointers/cow1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/19_smart_pointers/rc1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/20_threads/threads1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/20_threads/threads2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/20_threads/threads3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/21_macros/macros1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/21_macros/macros2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/21_macros/macros3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/21_macros/macros4.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/22_clippy/clippy1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/22_clippy/clippy2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/22_clippy/clippy3.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/23_conversions/as_ref_mut.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/23_conversions/from_into.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/23_conversions/from_str.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/23_conversions/try_from_into.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/23_conversions/using_as.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/quizzes/quiz1.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/quizzes/quiz2.rs
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions solutions/quizzes/quiz3.rs
@@ -0,0 +1 @@
// TODO

0 comments on commit 53fdb90

Please sign in to comment.