From 63e0fab85e6b80a7545ab747309aa290f11e2065 Mon Sep 17 00:00:00 2001 From: eveeifyeve <88671402+Eveeifyeve@users.noreply.github.com> Date: Sun, 5 Oct 2025 21:12:24 +1100 Subject: [PATCH 1/3] refactor: swap strings and modules to be consistant with book. Closes: https://github.com/rust-lang/rustlings/issues/1937 --- dev/Cargo.toml | 28 +++++++++---------- .../{10_modules => 09_modules}/README.md | 0 .../{10_modules => 09_modules}/modules1.rs | 0 .../{10_modules => 09_modules}/modules2.rs | 0 .../{10_modules => 09_modules}/modules3.rs | 0 .../{09_strings => 10_strings}/README.md | 0 .../{09_strings => 10_strings}/strings1.rs | 0 .../{09_strings => 10_strings}/strings2.rs | 0 .../{09_strings => 10_strings}/strings3.rs | 0 .../{09_strings => 10_strings}/strings4.rs | 0 rustlings-macros/info.toml | 14 +++++----- .../{10_modules => 09_modules}/modules1.rs | 0 .../{10_modules => 09_modules}/modules2.rs | 0 .../{10_modules => 09_modules}/modules3.rs | 0 .../{09_strings => 10_strings}/strings1.rs | 0 .../{09_strings => 10_strings}/strings2.rs | 0 .../{09_strings => 10_strings}/strings3.rs | 0 .../{09_strings => 10_strings}/strings4.rs | 0 18 files changed, 21 insertions(+), 21 deletions(-) rename exercises/{10_modules => 09_modules}/README.md (100%) rename exercises/{10_modules => 09_modules}/modules1.rs (100%) rename exercises/{10_modules => 09_modules}/modules2.rs (100%) rename exercises/{10_modules => 09_modules}/modules3.rs (100%) rename exercises/{09_strings => 10_strings}/README.md (100%) rename exercises/{09_strings => 10_strings}/strings1.rs (100%) rename exercises/{09_strings => 10_strings}/strings2.rs (100%) rename exercises/{09_strings => 10_strings}/strings3.rs (100%) rename exercises/{09_strings => 10_strings}/strings4.rs (100%) rename solutions/{10_modules => 09_modules}/modules1.rs (100%) rename solutions/{10_modules => 09_modules}/modules2.rs (100%) rename solutions/{10_modules => 09_modules}/modules3.rs (100%) rename solutions/{09_strings => 10_strings}/strings1.rs (100%) rename solutions/{09_strings => 10_strings}/strings2.rs (100%) rename solutions/{09_strings => 10_strings}/strings3.rs (100%) rename solutions/{09_strings => 10_strings}/strings4.rs (100%) diff --git a/dev/Cargo.toml b/dev/Cargo.toml index 4f725b704b..2d04379d99 100644 --- a/dev/Cargo.toml +++ b/dev/Cargo.toml @@ -72,20 +72,20 @@ bin = [ { name = "enums2_sol", path = "../solutions/08_enums/enums2.rs" }, { name = "enums3", path = "../exercises/08_enums/enums3.rs" }, { name = "enums3_sol", path = "../solutions/08_enums/enums3.rs" }, - { name = "strings1", path = "../exercises/09_strings/strings1.rs" }, - { name = "strings1_sol", path = "../solutions/09_strings/strings1.rs" }, - { name = "strings2", path = "../exercises/09_strings/strings2.rs" }, - { name = "strings2_sol", path = "../solutions/09_strings/strings2.rs" }, - { name = "strings3", path = "../exercises/09_strings/strings3.rs" }, - { name = "strings3_sol", path = "../solutions/09_strings/strings3.rs" }, - { name = "strings4", path = "../exercises/09_strings/strings4.rs" }, - { name = "strings4_sol", path = "../solutions/09_strings/strings4.rs" }, - { name = "modules1", path = "../exercises/10_modules/modules1.rs" }, - { name = "modules1_sol", path = "../solutions/10_modules/modules1.rs" }, - { name = "modules2", path = "../exercises/10_modules/modules2.rs" }, - { name = "modules2_sol", path = "../solutions/10_modules/modules2.rs" }, - { name = "modules3", path = "../exercises/10_modules/modules3.rs" }, - { name = "modules3_sol", path = "../solutions/10_modules/modules3.rs" }, + { name = "strings1", path = "../exercises/10_strings/strings1.rs" }, + { name = "strings1_sol", path = "../solutions/10_strings/strings1.rs" }, + { name = "strings2", path = "../exercises/10_strings/strings2.rs" }, + { name = "strings2_sol", path = "../solutions/10_strings/strings2.rs" }, + { name = "strings3", path = "../exercises/10_strings/strings3.rs" }, + { name = "strings3_sol", path = "../solutions/10_strings/strings3.rs" }, + { name = "strings4", path = "../exercises/10_strings/strings4.rs" }, + { name = "strings4_sol", path = "../solutions/10_strings/strings4.rs" }, + { name = "modules1", path = "../exercises/09_modules/modules1.rs" }, + { name = "modules1_sol", path = "../solutions/09_modules/modules1.rs" }, + { name = "modules2", path = "../exercises/09_modules/modules2.rs" }, + { name = "modules2_sol", path = "../solutions/09_modules/modules2.rs" }, + { name = "modules3", path = "../exercises/09_modules/modules3.rs" }, + { name = "modules3_sol", path = "../solutions/09_modules/modules3.rs" }, { name = "hashmaps1", path = "../exercises/11_hashmaps/hashmaps1.rs" }, { name = "hashmaps1_sol", path = "../solutions/11_hashmaps/hashmaps1.rs" }, { name = "hashmaps2", path = "../exercises/11_hashmaps/hashmaps2.rs" }, diff --git a/exercises/10_modules/README.md b/exercises/09_modules/README.md similarity index 100% rename from exercises/10_modules/README.md rename to exercises/09_modules/README.md diff --git a/exercises/10_modules/modules1.rs b/exercises/09_modules/modules1.rs similarity index 100% rename from exercises/10_modules/modules1.rs rename to exercises/09_modules/modules1.rs diff --git a/exercises/10_modules/modules2.rs b/exercises/09_modules/modules2.rs similarity index 100% rename from exercises/10_modules/modules2.rs rename to exercises/09_modules/modules2.rs diff --git a/exercises/10_modules/modules3.rs b/exercises/09_modules/modules3.rs similarity index 100% rename from exercises/10_modules/modules3.rs rename to exercises/09_modules/modules3.rs diff --git a/exercises/09_strings/README.md b/exercises/10_strings/README.md similarity index 100% rename from exercises/09_strings/README.md rename to exercises/10_strings/README.md diff --git a/exercises/09_strings/strings1.rs b/exercises/10_strings/strings1.rs similarity index 100% rename from exercises/09_strings/strings1.rs rename to exercises/10_strings/strings1.rs diff --git a/exercises/09_strings/strings2.rs b/exercises/10_strings/strings2.rs similarity index 100% rename from exercises/09_strings/strings2.rs rename to exercises/10_strings/strings2.rs diff --git a/exercises/09_strings/strings3.rs b/exercises/10_strings/strings3.rs similarity index 100% rename from exercises/09_strings/strings3.rs rename to exercises/10_strings/strings3.rs diff --git a/exercises/09_strings/strings4.rs b/exercises/10_strings/strings4.rs similarity index 100% rename from exercises/09_strings/strings4.rs rename to exercises/10_strings/strings4.rs diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index ca3ecf1f03..6b013b55ff 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -457,7 +457,7 @@ to get the variant's values.""" [[exercises]] name = "strings1" -dir = "09_strings" +dir = "10_strings" test = false hint = """ The `current_favorite_color` function is currently returning a string slice @@ -471,7 +471,7 @@ another way that uses the `From` trait.""" [[exercises]] name = "strings2" -dir = "09_strings" +dir = "10_strings" test = false hint = """ Yes, it would be really easy to fix this by just changing the value bound to @@ -486,7 +486,7 @@ https://doc.rust-lang.org/book/ch15-02-deref.html#implicit-deref-coercions-with- [[exercises]] name = "strings3" -dir = "09_strings" +dir = "10_strings" hint = """ There are many useful standard library functions for strings. Let's try and use some of them: @@ -500,7 +500,7 @@ https://doc.rust-lang.org/std/string/struct.String.html#method.replace""" [[exercises]] name = "strings4" -dir = "09_strings" +dir = "10_strings" test = false hint = """ Replace `placeholder` with either `string` or `string_slice` in the `main` @@ -516,7 +516,7 @@ because "blue" is `&str`, not `String`.""" [[exercises]] name = "modules1" -dir = "10_modules" +dir = "09_modules" test = false hint = """ Everything is private in Rust by default. But there's a keyword we can use @@ -524,7 +524,7 @@ to make something public!""" [[exercises]] name = "modules2" -dir = "10_modules" +dir = "09_modules" test = false hint = """ The `delicious_snacks` module is trying to present an external interface that @@ -537,7 +537,7 @@ https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-ke [[exercises]] name = "modules3" -dir = "10_modules" +dir = "09_modules" test = false hint = """ `UNIX_EPOCH` and `SystemTime` are declared in the `std::time` module. Add a diff --git a/solutions/10_modules/modules1.rs b/solutions/09_modules/modules1.rs similarity index 100% rename from solutions/10_modules/modules1.rs rename to solutions/09_modules/modules1.rs diff --git a/solutions/10_modules/modules2.rs b/solutions/09_modules/modules2.rs similarity index 100% rename from solutions/10_modules/modules2.rs rename to solutions/09_modules/modules2.rs diff --git a/solutions/10_modules/modules3.rs b/solutions/09_modules/modules3.rs similarity index 100% rename from solutions/10_modules/modules3.rs rename to solutions/09_modules/modules3.rs diff --git a/solutions/09_strings/strings1.rs b/solutions/10_strings/strings1.rs similarity index 100% rename from solutions/09_strings/strings1.rs rename to solutions/10_strings/strings1.rs diff --git a/solutions/09_strings/strings2.rs b/solutions/10_strings/strings2.rs similarity index 100% rename from solutions/09_strings/strings2.rs rename to solutions/10_strings/strings2.rs diff --git a/solutions/09_strings/strings3.rs b/solutions/10_strings/strings3.rs similarity index 100% rename from solutions/09_strings/strings3.rs rename to solutions/10_strings/strings3.rs diff --git a/solutions/09_strings/strings4.rs b/solutions/10_strings/strings4.rs similarity index 100% rename from solutions/09_strings/strings4.rs rename to solutions/10_strings/strings4.rs From 42b924aeab7c8621c3e064f2dd3a98f39946ffb3 Mon Sep 17 00:00:00 2001 From: eveeifyeve <88671402+Eveeifyeve@users.noreply.github.com> Date: Sun, 5 Oct 2025 21:37:09 +1100 Subject: [PATCH 2/3] feat: add exercise modules in diferent files/dirs closes: https://github.com/rust-lang/rustlings/issues/1143 --- dev/Cargo.toml | 2 ++ exercises/09_modules/fruit.rs | 3 +++ exercises/09_modules/modules4.rs | 5 +++++ rustlings-macros/info.toml | 12 ++++++++++++ solutions/09_modules/fruit.rs | 3 +++ solutions/09_modules/modules4.rs | 5 +++++ 6 files changed, 30 insertions(+) create mode 100644 exercises/09_modules/fruit.rs create mode 100644 exercises/09_modules/modules4.rs create mode 100644 solutions/09_modules/fruit.rs create mode 100644 solutions/09_modules/modules4.rs diff --git a/dev/Cargo.toml b/dev/Cargo.toml index 2d04379d99..2f4c83d3d0 100644 --- a/dev/Cargo.toml +++ b/dev/Cargo.toml @@ -86,6 +86,8 @@ bin = [ { name = "modules2_sol", path = "../solutions/09_modules/modules2.rs" }, { name = "modules3", path = "../exercises/09_modules/modules3.rs" }, { name = "modules3_sol", path = "../solutions/09_modules/modules3.rs" }, + { name = "modules4", path = "../exercises/09_modules/modules4.rs" }, + { name = "modules4_sol", path = "../solutions/09_modules/modules4.rs" }, { name = "hashmaps1", path = "../exercises/11_hashmaps/hashmaps1.rs" }, { name = "hashmaps1_sol", path = "../solutions/11_hashmaps/hashmaps1.rs" }, { name = "hashmaps2", path = "../exercises/11_hashmaps/hashmaps2.rs" }, diff --git a/exercises/09_modules/fruit.rs b/exercises/09_modules/fruit.rs new file mode 100644 index 0000000000..dceae23cb0 --- /dev/null +++ b/exercises/09_modules/fruit.rs @@ -0,0 +1,3 @@ +pub fn get_fav_fruit() { + println("Got your favourite fruit!") +} diff --git a/exercises/09_modules/modules4.rs b/exercises/09_modules/modules4.rs new file mode 100644 index 0000000000..12720e801f --- /dev/null +++ b/exercises/09_modules/modules4.rs @@ -0,0 +1,5 @@ +// TODO: fix the compiler error for missing modules. + +fn main() { + fruit::get_fav_fruit(); +} diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index 6b013b55ff..261c72f703 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -544,6 +544,18 @@ hint = """ `use` statement for these two to bring them into scope. You can use nested paths to bring these two in using only one line.""" +[[exercises]] +name = "modules4" +dir = "09_modules" +test = false +hint = """ +The mod.rs is trying to use the modules from the file cake.rs and fruit directory. +Complete the `mod` statements to fit the use in `main`. + +Learn more in The Book: +https://doc.rust-lang.org/book/ch07-05-separating-modules-into-different-files.html +""" + # HASHMAPS [[exercises]] diff --git a/solutions/09_modules/fruit.rs b/solutions/09_modules/fruit.rs new file mode 100644 index 0000000000..dceae23cb0 --- /dev/null +++ b/solutions/09_modules/fruit.rs @@ -0,0 +1,3 @@ +pub fn get_fav_fruit() { + println("Got your favourite fruit!") +} diff --git a/solutions/09_modules/modules4.rs b/solutions/09_modules/modules4.rs new file mode 100644 index 0000000000..b5b77fca17 --- /dev/null +++ b/solutions/09_modules/modules4.rs @@ -0,0 +1,5 @@ +mod fruit; + +fn main() { + fruit::get_fav_fruit(); +} From 0219ef7cc99168e02c5fb142bcc1657dd8c354d1 Mon Sep 17 00:00:00 2001 From: eveeifyeve <88671402+Eveeifyeve@users.noreply.github.com> Date: Sun, 5 Oct 2025 23:49:20 +1100 Subject: [PATCH 3/3] WIP: add one aditional module file for exercise support --- src/cargo_toml.rs | 2 ++ src/info_file.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/cargo_toml.rs b/src/cargo_toml.rs index ce0dfd0cf3..d624f86781 100644 --- a/src/cargo_toml.rs +++ b/src/cargo_toml.rs @@ -108,6 +108,7 @@ mod tests { ExerciseInfo { name: String::from("1"), dir: None, + module_file: None, test: true, strict_clippy: true, hint: String::new(), @@ -116,6 +117,7 @@ mod tests { ExerciseInfo { name: String::from("2"), dir: Some(String::from("d")), + module_file: None, test: false, strict_clippy: false, hint: String::new(), diff --git a/src/info_file.rs b/src/info_file.rs index 04e5d644a2..b0603dd93a 100644 --- a/src/info_file.rs +++ b/src/info_file.rs @@ -11,6 +11,9 @@ pub struct ExerciseInfo { pub name: String, /// Exercise's directory name inside the `exercises/` directory. pub dir: Option, + /// Extra one Aditional Module file + #[serde(default)] + pub module_file: Option, /// Run `cargo test` on the exercise. #[serde(default = "default_true")] pub test: bool, @@ -52,6 +55,33 @@ impl ExerciseInfo { path } + + pub fn module_file_path(&self) -> Option { + if let Some(module_path) = &self.module_file { + let mut path = if let Some(dir) = &self.dir { + // 14 = 10 + 1 + 3 + // exercises/ + / + .rs + let mut path = String::with_capacity(14 + dir.len() + module_path.len()); + path.push_str("exercises/"); + path.push_str(dir); + path.push('/'); + path + } else { + // 13 = 10 + 3 + // exercises/ + .rs + let mut path = String::with_capacity(13 + module_path.len()); + path.push_str("exercises/"); + path + }; + + path.push_str(&module_path); + path.push_str(".rs"); + + Some(path) + } else { + None + } + } } impl RunnableExercise for ExerciseInfo {