From fa1f239a702eb2c0b7e0115e986481156961bbc8 Mon Sep 17 00:00:00 2001 From: mo8it Date: Thu, 11 Apr 2024 02:51:02 +0200 Subject: Remove "I AM NOT DONE" and the verify mode and add AppState --- exercises/18_iterators/iterators5.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'exercises/18_iterators/iterators5.rs') diff --git a/exercises/18_iterators/iterators5.rs b/exercises/18_iterators/iterators5.rs index a062ee4..ceec536 100644 --- a/exercises/18_iterators/iterators5.rs +++ b/exercises/18_iterators/iterators5.rs @@ -11,8 +11,6 @@ // Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::collections::HashMap; #[derive(Clone, Copy, PartialEq, Eq)] -- cgit v1.2.3 From cb9f1ac9ce3c834b0cca964b6809b74508f80b54 Mon Sep 17 00:00:00 2001 From: mo8it Date: Wed, 17 Apr 2024 22:46:21 +0200 Subject: Require a main function in all exercises --- exercises/03_if/if1.rs | 4 ++ exercises/03_if/if2.rs | 4 ++ exercises/03_if/if3.rs | 4 ++ exercises/04_primitive_types/primitive_types4.rs | 4 ++ exercises/04_primitive_types/primitive_types6.rs | 4 ++ exercises/05_vecs/vecs1.rs | 4 ++ exercises/05_vecs/vecs2.rs | 4 ++ exercises/07_structs/structs1.rs | 4 ++ exercises/07_structs/structs2.rs | 4 ++ exercises/07_structs/structs3.rs | 4 ++ exercises/08_enums/enums3.rs | 4 ++ exercises/09_strings/strings3.rs | 4 ++ exercises/11_hashmaps/hashmaps1.rs | 4 ++ exercises/11_hashmaps/hashmaps2.rs | 6 +- exercises/11_hashmaps/hashmaps3.rs | 10 +++- exercises/12_options/options1.rs | 4 ++ exercises/12_options/options2.rs | 4 ++ exercises/13_error_handling/errors1.rs | 4 ++ exercises/13_error_handling/errors2.rs | 4 ++ exercises/13_error_handling/errors4.rs | 4 ++ exercises/13_error_handling/errors6.rs | 4 ++ exercises/14_generics/generics2.rs | 4 ++ exercises/15_traits/traits2.rs | 4 ++ exercises/15_traits/traits3.rs | 4 ++ exercises/15_traits/traits4.rs | 4 ++ exercises/17_tests/tests1.rs | 4 ++ exercises/17_tests/tests2.rs | 4 ++ exercises/17_tests/tests3.rs | 4 ++ exercises/17_tests/tests4.rs | 8 ++- exercises/18_iterators/iterators2.rs | 4 ++ exercises/18_iterators/iterators3.rs | 4 ++ exercises/18_iterators/iterators4.rs | 4 ++ exercises/18_iterators/iterators5.rs | 4 ++ exercises/19_smart_pointers/cow1.rs | 4 ++ exercises/23_conversions/as_ref_mut.rs | 4 ++ exercises/quiz1.rs | 4 ++ exercises/quiz2.rs | 4 ++ exercises/quiz3.rs | 10 +++- src/dev/check.rs | 73 ++++++++++++------------ 39 files changed, 199 insertions(+), 44 deletions(-) (limited to 'exercises/18_iterators/iterators5.rs') diff --git a/exercises/03_if/if1.rs b/exercises/03_if/if1.rs index a1df66b..dbd0d28 100644 --- a/exercises/03_if/if1.rs +++ b/exercises/03_if/if1.rs @@ -10,6 +10,10 @@ pub fn bigger(a: i32, b: i32) -> i32 { // - additional variables } +fn main() { + // You can optionally experiment here. +} + // Don't mind this for now :) #[cfg(test)] mod tests { diff --git a/exercises/03_if/if2.rs b/exercises/03_if/if2.rs index 7b9c05f..a1ed5c8 100644 --- a/exercises/03_if/if2.rs +++ b/exercises/03_if/if2.rs @@ -13,6 +13,10 @@ pub fn foo_if_fizz(fizzish: &str) -> &str { } } +fn main() { + // You can optionally experiment here. +} + // No test changes needed! #[cfg(test)] mod tests { diff --git a/exercises/03_if/if3.rs b/exercises/03_if/if3.rs index caba172..0b44c5a 100644 --- a/exercises/03_if/if3.rs +++ b/exercises/03_if/if3.rs @@ -27,6 +27,10 @@ pub fn animal_habitat(animal: &str) -> &'static str { habitat } +fn main() { + // You can optionally experiment here. +} + // No test changes needed. #[cfg(test)] mod tests { diff --git a/exercises/04_primitive_types/primitive_types4.rs b/exercises/04_primitive_types/primitive_types4.rs index 8ed0a82..f99d889 100644 --- a/exercises/04_primitive_types/primitive_types4.rs +++ b/exercises/04_primitive_types/primitive_types4.rs @@ -5,6 +5,10 @@ // Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand // for a hint. +fn main() { + // You can optionally experiment here. +} + #[test] fn slice_out_of_array() { let a = [1, 2, 3, 4, 5]; diff --git a/exercises/04_primitive_types/primitive_types6.rs b/exercises/04_primitive_types/primitive_types6.rs index 5f82f10..48e84d3 100644 --- a/exercises/04_primitive_types/primitive_types6.rs +++ b/exercises/04_primitive_types/primitive_types6.rs @@ -6,6 +6,10 @@ // Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand // for a hint. +fn main() { + // You can optionally experiment here. +} + #[test] fn indexing_tuple() { let numbers = (1, 2, 3); diff --git a/exercises/05_vecs/vecs1.rs b/exercises/05_vecs/vecs1.rs index c64acbb..5f44cb2 100644 --- a/exercises/05_vecs/vecs1.rs +++ b/exercises/05_vecs/vecs1.rs @@ -14,6 +14,10 @@ fn array_and_vec() -> ([i32; 4], Vec) { (a, v) } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/05_vecs/vecs2.rs b/exercises/05_vecs/vecs2.rs index d64d3d1..1b16f0b 100644 --- a/exercises/05_vecs/vecs2.rs +++ b/exercises/05_vecs/vecs2.rs @@ -26,6 +26,10 @@ fn vec_map(v: &Vec) -> Vec { }).collect() } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/07_structs/structs1.rs b/exercises/07_structs/structs1.rs index 2978121..cd8b81c 100644 --- a/exercises/07_structs/structs1.rs +++ b/exercises/07_structs/structs1.rs @@ -14,6 +14,10 @@ struct ColorTupleStruct(/* TODO: Something goes here */); #[derive(Debug)] struct UnitLikeStruct; +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/07_structs/structs2.rs b/exercises/07_structs/structs2.rs index a7a2dec..7e61e75 100644 --- a/exercises/07_structs/structs2.rs +++ b/exercises/07_structs/structs2.rs @@ -28,6 +28,10 @@ fn create_order_template() -> Order { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/07_structs/structs3.rs b/exercises/07_structs/structs3.rs index 9835b81..bd562a1 100644 --- a/exercises/07_structs/structs3.rs +++ b/exercises/07_structs/structs3.rs @@ -38,6 +38,10 @@ impl Package { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/08_enums/enums3.rs b/exercises/08_enums/enums3.rs index 580a553..56c04fe 100644 --- a/exercises/08_enums/enums3.rs +++ b/exercises/08_enums/enums3.rs @@ -45,6 +45,10 @@ impl State { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/09_strings/strings3.rs b/exercises/09_strings/strings3.rs index dedc081..d53f654 100644 --- a/exercises/09_strings/strings3.rs +++ b/exercises/09_strings/strings3.rs @@ -18,6 +18,10 @@ fn replace_me(input: &str) -> String { ??? } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/11_hashmaps/hashmaps1.rs b/exercises/11_hashmaps/hashmaps1.rs index 5a52f61..51146df 100644 --- a/exercises/11_hashmaps/hashmaps1.rs +++ b/exercises/11_hashmaps/hashmaps1.rs @@ -24,6 +24,10 @@ fn fruit_basket() -> HashMap { basket } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/11_hashmaps/hashmaps2.rs b/exercises/11_hashmaps/hashmaps2.rs index 2730643..47983f6 100644 --- a/exercises/11_hashmaps/hashmaps2.rs +++ b/exercises/11_hashmaps/hashmaps2.rs @@ -41,6 +41,10 @@ fn fruit_basket(basket: &mut HashMap) { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; @@ -79,7 +83,7 @@ mod tests { let count = basket.values().sum::(); assert!(count > 11); } - + #[test] fn all_fruit_types_in_basket() { let mut basket = get_fruit_basket(); diff --git a/exercises/11_hashmaps/hashmaps3.rs b/exercises/11_hashmaps/hashmaps3.rs index 775a401..3322909 100644 --- a/exercises/11_hashmaps/hashmaps3.rs +++ b/exercises/11_hashmaps/hashmaps3.rs @@ -5,9 +5,9 @@ // Example: England,France,4,2 (England scored 4 goals, France 2). // // You have to build a scores table containing the name of the team, the total -// number of goals the team scored, and the total number of 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, +// number of goals the team scored, and the total number of 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! @@ -42,6 +42,10 @@ fn build_scores_table(results: String) -> HashMap { scores } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/12_options/options1.rs b/exercises/12_options/options1.rs index ba4b1cd..aecb123 100644 --- a/exercises/12_options/options1.rs +++ b/exercises/12_options/options1.rs @@ -14,6 +14,10 @@ fn maybe_icecream(time_of_day: u16) -> Option { ??? } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/12_options/options2.rs b/exercises/12_options/options2.rs index 73f707e..d183d1d 100644 --- a/exercises/12_options/options2.rs +++ b/exercises/12_options/options2.rs @@ -3,6 +3,10 @@ // Execute `rustlings hint options2` or use the `hint` watch subcommand for a // hint. +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { #[test] diff --git a/exercises/13_error_handling/errors1.rs b/exercises/13_error_handling/errors1.rs index 9767f2c..7991c42 100644 --- a/exercises/13_error_handling/errors1.rs +++ b/exercises/13_error_handling/errors1.rs @@ -9,6 +9,10 @@ // Execute `rustlings hint errors1` or use the `hint` watch subcommand for a // hint. +fn main() { + // You can optionally experiment here. +} + pub fn generate_nametag_text(name: String) -> Option { if name.is_empty() { // Empty names aren't allowed. diff --git a/exercises/13_error_handling/errors2.rs b/exercises/13_error_handling/errors2.rs index 88d1bf4..051516b 100644 --- a/exercises/13_error_handling/errors2.rs +++ b/exercises/13_error_handling/errors2.rs @@ -29,6 +29,10 @@ pub fn total_cost(item_quantity: &str) -> Result { Ok(qty * cost_per_item + processing_fee) } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/13_error_handling/errors4.rs b/exercises/13_error_handling/errors4.rs index 0e5c08b..9449417 100644 --- a/exercises/13_error_handling/errors4.rs +++ b/exercises/13_error_handling/errors4.rs @@ -19,6 +19,10 @@ impl PositiveNonzeroInteger { } } +fn main() { + // You can optionally experiment here. +} + #[test] fn test_creation() { assert!(PositiveNonzeroInteger::new(10).is_ok()); diff --git a/exercises/13_error_handling/errors6.rs b/exercises/13_error_handling/errors6.rs index de73a9a..363a3b9 100644 --- a/exercises/13_error_handling/errors6.rs +++ b/exercises/13_error_handling/errors6.rs @@ -54,6 +54,10 @@ impl PositiveNonzeroInteger { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod test { use super::*; diff --git a/exercises/14_generics/generics2.rs b/exercises/14_generics/generics2.rs index d50ed17..068468b 100644 --- a/exercises/14_generics/generics2.rs +++ b/exercises/14_generics/generics2.rs @@ -16,6 +16,10 @@ impl Wrapper { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/15_traits/traits2.rs b/exercises/15_traits/traits2.rs index 9a2bc07..18ebcb0 100644 --- a/exercises/15_traits/traits2.rs +++ b/exercises/15_traits/traits2.rs @@ -14,6 +14,10 @@ trait AppendBar { // TODO: Implement trait `AppendBar` for a vector of strings. +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/15_traits/traits3.rs b/exercises/15_traits/traits3.rs index 357f1d7..8412afa 100644 --- a/exercises/15_traits/traits3.rs +++ b/exercises/15_traits/traits3.rs @@ -23,6 +23,10 @@ struct OtherSoftware { impl Licensed for SomeSoftware {} // Don't edit this line impl Licensed for OtherSoftware {} // Don't edit this line +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/15_traits/traits4.rs b/exercises/15_traits/traits4.rs index 7242c48..18db0d6 100644 --- a/exercises/15_traits/traits4.rs +++ b/exercises/15_traits/traits4.rs @@ -25,6 +25,10 @@ fn compare_license_types(software: ??, software_two: ??) -> bool { software.licensing_info() == software_two.licensing_info() } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/17_tests/tests1.rs b/exercises/17_tests/tests1.rs index bde2108..d32ace1 100644 --- a/exercises/17_tests/tests1.rs +++ b/exercises/17_tests/tests1.rs @@ -10,6 +10,10 @@ // Execute `rustlings hint tests1` or use the `hint` watch subcommand for a // hint. +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { #[test] diff --git a/exercises/17_tests/tests2.rs b/exercises/17_tests/tests2.rs index aea5c0e..501c44b 100644 --- a/exercises/17_tests/tests2.rs +++ b/exercises/17_tests/tests2.rs @@ -6,6 +6,10 @@ // Execute `rustlings hint tests2` or use the `hint` watch subcommand for a // hint. +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { #[test] diff --git a/exercises/17_tests/tests3.rs b/exercises/17_tests/tests3.rs index d815e05..a2093cf 100644 --- a/exercises/17_tests/tests3.rs +++ b/exercises/17_tests/tests3.rs @@ -11,6 +11,10 @@ pub fn is_even(num: i32) -> bool { num % 2 == 0 } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/17_tests/tests4.rs b/exercises/17_tests/tests4.rs index 0972a5b..a50323c 100644 --- a/exercises/17_tests/tests4.rs +++ b/exercises/17_tests/tests4.rs @@ -7,7 +7,7 @@ struct Rectangle { width: i32, - height: i32 + height: i32, } impl Rectangle { @@ -16,10 +16,14 @@ impl Rectangle { if width <= 0 || height <= 0 { panic!("Rectangle width and height cannot be negative!") } - Rectangle {width, height} + Rectangle { width, height } } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/18_iterators/iterators2.rs b/exercises/18_iterators/iterators2.rs index 4ca7742..0ebd69a 100644 --- a/exercises/18_iterators/iterators2.rs +++ b/exercises/18_iterators/iterators2.rs @@ -33,6 +33,10 @@ pub fn capitalize_words_string(words: &[&str]) -> String { String::new() } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/18_iterators/iterators3.rs b/exercises/18_iterators/iterators3.rs index f7da049..3f5923c 100644 --- a/exercises/18_iterators/iterators3.rs +++ b/exercises/18_iterators/iterators3.rs @@ -43,6 +43,10 @@ fn list_of_results() -> () { let division_results = numbers.into_iter().map(|n| divide(n, 27)); } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/18_iterators/iterators4.rs b/exercises/18_iterators/iterators4.rs index af3958c..8fc8792 100644 --- a/exercises/18_iterators/iterators4.rs +++ b/exercises/18_iterators/iterators4.rs @@ -15,6 +15,10 @@ pub fn factorial(num: u64) -> u64 { // Execute `rustlings hint iterators4` for hints. } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/18_iterators/iterators5.rs b/exercises/18_iterators/iterators5.rs index ceec536..2604004 100644 --- a/exercises/18_iterators/iterators5.rs +++ b/exercises/18_iterators/iterators5.rs @@ -55,6 +55,10 @@ fn count_collection_iterator(collection: &[HashMap], value: Pr todo!(); } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/19_smart_pointers/cow1.rs b/exercises/19_smart_pointers/cow1.rs index b24591b..51e5fdb 100644 --- a/exercises/19_smart_pointers/cow1.rs +++ b/exercises/19_smart_pointers/cow1.rs @@ -25,6 +25,10 @@ fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> { input } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/23_conversions/as_ref_mut.rs b/exercises/23_conversions/as_ref_mut.rs index cd2c93b..6fb7c2f 100644 --- a/exercises/23_conversions/as_ref_mut.rs +++ b/exercises/23_conversions/as_ref_mut.rs @@ -26,6 +26,10 @@ fn num_sq(arg: &mut T) { ??? } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/exercises/quiz1.rs b/exercises/quiz1.rs index b9e71f5..55bc61f 100644 --- a/exercises/quiz1.rs +++ b/exercises/quiz1.rs @@ -16,6 +16,10 @@ // Put your function here! // fn calculate_price_of_apples { +fn main() { + // You can optionally experiment here. +} + // Don't modify this function! #[test] fn verify_test() { diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 8ace3fe..1d73ab9 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -40,6 +40,10 @@ mod my_module { } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { // TODO: What do we need to import to have `transformer` in scope? diff --git a/exercises/quiz3.rs b/exercises/quiz3.rs index 24f7082..780e130 100644 --- a/exercises/quiz3.rs +++ b/exercises/quiz3.rs @@ -24,11 +24,17 @@ pub struct ReportCard { impl ReportCard { pub fn print(&self) -> String { - format!("{} ({}) - achieved a grade of {}", - &self.student_name, &self.student_age, &self.grade) + format!( + "{} ({}) - achieved a grade of {}", + &self.student_name, &self.student_age, &self.grade + ) } } +fn main() { + // You can optionally experiment here. +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/dev/check.rs b/src/dev/check.rs index 4688e04..d2e5fe1 100644 --- a/src/dev/check.rs +++ b/src/dev/check.rs @@ -1,8 +1,9 @@ -use anyhow::{bail, Context, Result}; +use anyhow::{anyhow, bail, Context, Error, Result}; use std::{ cmp::Ordering, - fs::{self, read_dir}, - path::PathBuf, + fs::{self, read_dir, OpenOptions}, + io::Read, + path::{Path, PathBuf}, }; use crate::{ @@ -18,6 +19,7 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result Result Result, -) -> Result> { - let mut names = hashbrown::HashSet::with_capacity(info_file.exercises.len()); +fn unexpected_file(path: &Path) -> Error { + anyhow!("Found the file `{}`. Only `README.md` and Rust files related to an exercise in `info.toml` are allowed in the `exercises` directory", path.display()) +} +fn check_exercise_dir_files(info_file_paths: hashbrown::HashSet) -> Result<()> { for entry in read_dir("exercises").context("Failed to open the `exercises` directory")? { let entry = entry.context("Failed to read the `exercises` directory")?; @@ -72,11 +91,9 @@ fn check_exercise_dir_files( } if !info_file_paths.contains(&path) { - bail!("`{}` is expected to be an exercise file corresponding to some exercise in `info.toml`", path.display()); + return Err(unexpected_file(&path)); } - let file_name = file_name.to_string_lossy(); - names.insert(file_name[..file_name.len() - 3].to_string()); continue; } @@ -89,7 +106,7 @@ fn check_exercise_dir_files( let path = entry.path(); if !entry.file_type().unwrap().is_file() { - bail!("Found {} but expected only files", path.display()); + bail!("Found `{}` but expected only files. Only one level of exercise nesting is allowed", path.display()); } let file_name = path.file_name().unwrap(); @@ -98,21 +115,15 @@ fn check_exercise_dir_files( } if !info_file_paths.contains(&path) { - bail!("`{}` is expected to be an exercise file corresponding to some exercise in `info.toml`", path.display()); + return Err(unexpected_file(&path)); } - - // The file name must be valid Unicode with the `.rs` extension - // because it is part of the info file paths. - let file_name = file_name.to_string_lossy(); - let file_name_without_rs_extension = file_name[..file_name.len() - 3].to_string(); - names.insert(file_name_without_rs_extension); } } - Ok(names) + Ok(()) } -fn check_info_file(info_file: &InfoFile) -> Result<()> { +fn check_exercises(info_file: &InfoFile) -> 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"), @@ -120,17 +131,7 @@ fn check_info_file(info_file: &InfoFile) -> Result<()> { } let info_file_paths = check_info_file_exercises(info_file)?; - let names_in_exercises_dir = check_exercise_dir_files(info_file, info_file_paths)?; - - // Now, we know that every file has an exercise in `info.toml`. - // But we need to check that every exercise in `info.toml` has a file. - if names_in_exercises_dir.len() != info_file.exercises.len() { - for exercise_info in &info_file.exercises { - if !names_in_exercises_dir.contains(&exercise_info.name) { - bail!("The file `{}` is missing", exercise_info.path()); - } - } - } + check_exercise_dir_files(info_file_paths)?; Ok(()) } @@ -190,7 +191,7 @@ fn check_cargo_toml( pub fn check() -> Result<()> { let info_file = InfoFile::parse()?; - check_info_file(&info_file)?; + check_exercises(&info_file)?; if DEVELOPING_OFFICIAL_RUSTLINGS { check_cargo_toml( -- cgit v1.2.3 From 2f810a4da67233716ad93e00afff6e8b260f4807 Mon Sep 17 00:00:00 2001 From: mo8it Date: Wed, 17 Apr 2024 23:34:27 +0200 Subject: Clean up and unify exercises --- exercises/00_intro/intro1.rs | 5 -- exercises/00_intro/intro2.rs | 5 -- exercises/01_variables/variables1.rs | 5 -- exercises/01_variables/variables2.rs | 5 -- exercises/01_variables/variables3.rs | 5 -- exercises/01_variables/variables4.rs | 5 -- exercises/01_variables/variables5.rs | 5 -- exercises/01_variables/variables6.rs | 5 -- exercises/02_functions/functions1.rs | 5 -- exercises/02_functions/functions2.rs | 5 -- exercises/02_functions/functions3.rs | 5 -- exercises/02_functions/functions4.rs | 5 -- exercises/02_functions/functions5.rs | 5 -- exercises/03_if/if1.rs | 4 - exercises/03_if/if2.rs | 4 - exercises/03_if/if3.rs | 4 - exercises/04_primitive_types/primitive_types1.rs | 5 +- exercises/04_primitive_types/primitive_types2.rs | 5 -- exercises/04_primitive_types/primitive_types3.rs | 5 -- exercises/04_primitive_types/primitive_types4.rs | 20 ++--- exercises/04_primitive_types/primitive_types5.rs | 5 -- exercises/04_primitive_types/primitive_types6.rs | 24 ++--- exercises/05_vecs/vecs1.rs | 4 - exercises/05_vecs/vecs2.rs | 4 - exercises/06_move_semantics/move_semantics1.rs | 30 ++++--- exercises/06_move_semantics/move_semantics2.rs | 34 +++---- exercises/06_move_semantics/move_semantics3.rs | 30 ++++--- exercises/06_move_semantics/move_semantics4.rs | 34 +++---- exercises/06_move_semantics/move_semantics5.rs | 30 ++++--- exercises/06_move_semantics/move_semantics6.rs | 5 -- exercises/07_structs/structs1.rs | 5 -- exercises/07_structs/structs2.rs | 5 -- exercises/07_structs/structs3.rs | 5 -- exercises/08_enums/enums1.rs | 4 - exercises/08_enums/enums2.rs | 5 -- exercises/08_enums/enums3.rs | 5 -- exercises/09_strings/strings1.rs | 5 -- exercises/09_strings/strings2.rs | 5 -- exercises/09_strings/strings3.rs | 5 -- exercises/09_strings/strings4.rs | 6 +- exercises/10_modules/modules1.rs | 5 -- exercises/10_modules/modules2.rs | 5 -- exercises/10_modules/modules3.rs | 5 -- exercises/11_hashmaps/hashmaps1.rs | 5 -- exercises/11_hashmaps/hashmaps2.rs | 5 -- exercises/11_hashmaps/hashmaps3.rs | 5 -- exercises/12_options/options1.rs | 5 -- exercises/12_options/options2.rs | 5 -- exercises/12_options/options3.rs | 5 -- exercises/13_error_handling/errors1.rs | 5 -- exercises/13_error_handling/errors2.rs | 5 -- exercises/13_error_handling/errors3.rs | 5 -- exercises/13_error_handling/errors4.rs | 26 +++--- exercises/13_error_handling/errors5.rs | 5 -- exercises/13_error_handling/errors6.rs | 5 -- exercises/14_generics/generics1.rs | 5 -- exercises/14_generics/generics2.rs | 5 -- exercises/15_traits/traits1.rs | 5 -- exercises/15_traits/traits2.rs | 4 - exercises/15_traits/traits3.rs | 5 -- exercises/15_traits/traits4.rs | 5 -- exercises/15_traits/traits5.rs | 5 -- exercises/16_lifetimes/lifetimes1.rs | 5 -- exercises/16_lifetimes/lifetimes2.rs | 5 -- exercises/16_lifetimes/lifetimes3.rs | 10 +-- exercises/17_tests/tests1.rs | 5 -- exercises/17_tests/tests2.rs | 5 -- exercises/17_tests/tests3.rs | 5 -- exercises/17_tests/tests4.rs | 5 -- exercises/18_iterators/iterators1.rs | 32 ++++--- exercises/18_iterators/iterators2.rs | 5 -- exercises/18_iterators/iterators3.rs | 5 -- exercises/18_iterators/iterators4.rs | 6 -- exercises/18_iterators/iterators5.rs | 5 -- exercises/19_smart_pointers/arc1.rs | 4 - exercises/19_smart_pointers/box1.rs | 4 - exercises/19_smart_pointers/cow1.rs | 4 - exercises/19_smart_pointers/rc1.rs | 109 ++++++++++++----------- exercises/20_threads/threads1.rs | 5 -- exercises/20_threads/threads2.rs | 5 -- exercises/20_threads/threads3.rs | 38 ++++---- exercises/21_macros/macros1.rs | 5 -- exercises/21_macros/macros2.rs | 5 -- exercises/21_macros/macros3.rs | 5 -- exercises/21_macros/macros4.rs | 5 -- exercises/22_clippy/clippy1.rs | 5 -- exercises/22_clippy/clippy2.rs | 5 -- exercises/22_clippy/clippy3.rs | 3 - exercises/23_conversions/as_ref_mut.rs | 5 -- exercises/23_conversions/from_into.rs | 6 -- exercises/23_conversions/from_str.rs | 5 -- exercises/23_conversions/try_from_into.rs | 5 -- exercises/23_conversions/using_as.rs | 5 -- exercises/quiz1.rs | 31 +++---- exercises/quiz2.rs | 4 - exercises/quiz3.rs | 4 - 96 files changed, 242 insertions(+), 610 deletions(-) (limited to 'exercises/18_iterators/iterators5.rs') diff --git a/exercises/00_intro/intro1.rs b/exercises/00_intro/intro1.rs index 170d195..7000039 100644 --- a/exercises/00_intro/intro1.rs +++ b/exercises/00_intro/intro1.rs @@ -1,5 +1,3 @@ -// intro1.rs -// // 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. @@ -8,9 +6,6 @@ // 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. fn main() { println!("Hello and"); diff --git a/exercises/00_intro/intro2.rs b/exercises/00_intro/intro2.rs index 84e0d75..c7a3ab2 100644 --- a/exercises/00_intro/intro2.rs +++ b/exercises/00_intro/intro2.rs @@ -1,9 +1,4 @@ -// intro2.rs -// // Make the code print a greeting to the world. -// -// Execute `rustlings hint intro2` or use the `hint` watch subcommand for a -// hint. fn main() { printline!("Hello there!") diff --git a/exercises/01_variables/variables1.rs b/exercises/01_variables/variables1.rs index 56408f3..3035bfa 100644 --- a/exercises/01_variables/variables1.rs +++ b/exercises/01_variables/variables1.rs @@ -1,9 +1,4 @@ -// variables1.rs -// // Make me compile! -// -// Execute `rustlings hint variables1` or use the `hint` watch subcommand for a -// hint. fn main() { x = 5; diff --git a/exercises/01_variables/variables2.rs b/exercises/01_variables/variables2.rs index 0f417e0..ce2dd85 100644 --- a/exercises/01_variables/variables2.rs +++ b/exercises/01_variables/variables2.rs @@ -1,8 +1,3 @@ -// variables2.rs -// -// Execute `rustlings hint variables2` or use the `hint` watch subcommand for a -// hint. - fn main() { let x; if x == 10 { diff --git a/exercises/01_variables/variables3.rs b/exercises/01_variables/variables3.rs index 421c6b1..488385b 100644 --- a/exercises/01_variables/variables3.rs +++ b/exercises/01_variables/variables3.rs @@ -1,8 +1,3 @@ -// variables3.rs -// -// Execute `rustlings hint variables3` or use the `hint` watch subcommand for a -// hint. - fn main() { let x: i32; println!("Number {}", x); diff --git a/exercises/01_variables/variables4.rs b/exercises/01_variables/variables4.rs index 68f8f50..67be127 100644 --- a/exercises/01_variables/variables4.rs +++ b/exercises/01_variables/variables4.rs @@ -1,8 +1,3 @@ -// variables4.rs -// -// Execute `rustlings hint variables4` or use the `hint` watch subcommand for a -// hint. - fn main() { let x = 3; println!("Number {}", x); diff --git a/exercises/01_variables/variables5.rs b/exercises/01_variables/variables5.rs index 7014c56..3a74541 100644 --- a/exercises/01_variables/variables5.rs +++ b/exercises/01_variables/variables5.rs @@ -1,8 +1,3 @@ -// variables5.rs -// -// Execute `rustlings hint variables5` or use the `hint` watch subcommand for a -// hint. - fn main() { let number = "T-H-R-E-E"; // don't change this line println!("Spell a Number : {}", number); diff --git a/exercises/01_variables/variables6.rs b/exercises/01_variables/variables6.rs index 9f47682..4746331 100644 --- a/exercises/01_variables/variables6.rs +++ b/exercises/01_variables/variables6.rs @@ -1,8 +1,3 @@ -// variables6.rs -// -// Execute `rustlings hint variables6` or use the `hint` watch subcommand for a -// hint. - const NUMBER = 3; fn main() { println!("Number {}", NUMBER); diff --git a/exercises/02_functions/functions1.rs b/exercises/02_functions/functions1.rs index 2365f91..4e3b103 100644 --- a/exercises/02_functions/functions1.rs +++ b/exercises/02_functions/functions1.rs @@ -1,8 +1,3 @@ -// functions1.rs -// -// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a -// hint. - fn main() { call_me(); } diff --git a/exercises/02_functions/functions2.rs b/exercises/02_functions/functions2.rs index 64dbd66..84e09cd 100644 --- a/exercises/02_functions/functions2.rs +++ b/exercises/02_functions/functions2.rs @@ -1,8 +1,3 @@ -// functions2.rs -// -// Execute `rustlings hint functions2` or use the `hint` watch subcommand for a -// hint. - fn main() { call_me(3); } diff --git a/exercises/02_functions/functions3.rs b/exercises/02_functions/functions3.rs index 5037121..66fb6d3 100644 --- a/exercises/02_functions/functions3.rs +++ b/exercises/02_functions/functions3.rs @@ -1,8 +1,3 @@ -// functions3.rs -// -// Execute `rustlings hint functions3` or use the `hint` watch subcommand for a -// hint. - fn main() { call_me(); } diff --git a/exercises/02_functions/functions4.rs b/exercises/02_functions/functions4.rs index 6b449ed..06d3b1b 100644 --- a/exercises/02_functions/functions4.rs +++ b/exercises/02_functions/functions4.rs @@ -1,12 +1,7 @@ -// functions4.rs -// // 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. fn main() { let original_price = 51; diff --git a/exercises/02_functions/functions5.rs b/exercises/02_functions/functions5.rs index 0c96322..3bb5e52 100644 --- a/exercises/02_functions/functions5.rs +++ b/exercises/02_functions/functions5.rs @@ -1,8 +1,3 @@ -// functions5.rs -// -// Execute `rustlings hint functions5` or use the `hint` watch subcommand for a -// hint. - fn main() { let answer = square(3); println!("The square of 3 is {}", answer); diff --git a/exercises/03_if/if1.rs b/exercises/03_if/if1.rs index dbd0d28..52dee0b 100644 --- a/exercises/03_if/if1.rs +++ b/exercises/03_if/if1.rs @@ -1,7 +1,3 @@ -// if1.rs -// -// Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint. - pub fn bigger(a: i32, b: i32) -> i32 { // Complete this function to return the bigger number! // If both numbers are equal, any of them can be returned. diff --git a/exercises/03_if/if2.rs b/exercises/03_if/if2.rs index a1ed5c8..a06bba5 100644 --- a/exercises/03_if/if2.rs +++ b/exercises/03_if/if2.rs @@ -1,9 +1,5 @@ -// 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. pub fn foo_if_fizz(fizzish: &str) -> &str { if fizzish == "fizz" { diff --git a/exercises/03_if/if3.rs b/exercises/03_if/if3.rs index 0b44c5a..1d9b7c2 100644 --- a/exercises/03_if/if3.rs +++ b/exercises/03_if/if3.rs @@ -1,7 +1,3 @@ -// if3.rs -// -// Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint. - pub fn animal_habitat(animal: &str) -> &'static str { let identifier = if animal == "crab" { 1 diff --git a/exercises/04_primitive_types/primitive_types1.rs b/exercises/04_primitive_types/primitive_types1.rs index f9169c8..0002651 100644 --- a/exercises/04_primitive_types/primitive_types1.rs +++ b/exercises/04_primitive_types/primitive_types1.rs @@ -1,7 +1,4 @@ -// primitive_types1.rs -// -// Fill in the rest of the line that has code missing! No hints, there's no -// tricks, just get used to typing these :) +// Fill in the rest of the line that has code missing! fn main() { // Booleans (`bool`) diff --git a/exercises/04_primitive_types/primitive_types2.rs b/exercises/04_primitive_types/primitive_types2.rs index 1911b12..29c7471 100644 --- a/exercises/04_primitive_types/primitive_types2.rs +++ b/exercises/04_primitive_types/primitive_types2.rs @@ -1,8 +1,3 @@ -// primitive_types2.rs -// -// Fill in the rest of the line that has code missing! No hints, there's no -// tricks, just get used to typing these :) - fn main() { // Characters (`char`) diff --git a/exercises/04_primitive_types/primitive_types3.rs b/exercises/04_primitive_types/primitive_types3.rs index 70a8cc2..5095fc4 100644 --- a/exercises/04_primitive_types/primitive_types3.rs +++ b/exercises/04_primitive_types/primitive_types3.rs @@ -1,9 +1,4 @@ -// primitive_types3.rs -// // Create an array with at least 100 elements in it where the ??? is. -// -// Execute `rustlings hint primitive_types3` or use the `hint` watch subcommand -// for a hint. fn main() { let a = ??? diff --git a/exercises/04_primitive_types/primitive_types4.rs b/exercises/04_primitive_types/primitive_types4.rs index f99d889..c583ae1 100644 --- a/exercises/04_primitive_types/primitive_types4.rs +++ b/exercises/04_primitive_types/primitive_types4.rs @@ -1,19 +1,19 @@ -// primitive_types4.rs -// // Get a slice out of Array a where the ??? is so that the test passes. -// -// Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand -// for a hint. fn main() { // You can optionally experiment here. } -#[test] -fn slice_out_of_array() { - let a = [1, 2, 3, 4, 5]; +#[cfg(test)] +mod tests { + use super::*; - let nice_slice = ??? + #[test] + fn slice_out_of_array() { + let a = [1, 2, 3, 4, 5]; - assert_eq!([2, 3, 4], nice_slice) + let nice_slice = ??? + + assert_eq!([2, 3, 4], nice_slice) + } } diff --git a/exercises/04_primitive_types/primitive_types5.rs b/exercises/04_primitive_types/primitive_types5.rs index 5754a3d..f2216a5 100644 --- a/exercises/04_primitive_types/primitive_types5.rs +++ b/exercises/04_primitive_types/primitive_types5.rs @@ -1,9 +1,4 @@ -// primitive_types5.rs -// // Destructure the `cat` tuple so that the println will work. -// -// Execute `rustlings hint primitive_types5` or use the `hint` watch subcommand -// for a hint. fn main() { let cat = ("Furry McFurson", 3.5); diff --git a/exercises/04_primitive_types/primitive_types6.rs b/exercises/04_primitive_types/primitive_types6.rs index 48e84d3..83cec24 100644 --- a/exercises/04_primitive_types/primitive_types6.rs +++ b/exercises/04_primitive_types/primitive_types6.rs @@ -1,21 +1,21 @@ -// primitive_types6.rs -// // Use a tuple index to access the second element of `numbers`. You can put the // expression for the second element where ??? is so that the test passes. -// -// Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand -// for a hint. fn main() { // You can optionally experiment here. } -#[test] -fn indexing_tuple() { - let numbers = (1, 2, 3); - // Replace below ??? with the tuple indexing syntax. - let second = ???; +#[cfg(test)] +mod tests { + use super::*; - assert_eq!(2, second, - "This is not the 2nd number in the tuple!") + #[test] + fn indexing_tuple() { + let numbers = (1, 2, 3); + // Replace below ??? with the tuple indexing syntax. + let second = ???; + + assert_eq!(2, second, + "This is not the 2nd number in the tuple!") + } } diff --git a/exercises/05_vecs/vecs1.rs b/exercises/05_vecs/vecs1.rs index 5f44cb2..ddcad84 100644 --- a/exercises/05_vecs/vecs1.rs +++ b/exercises/05_vecs/vecs1.rs @@ -1,11 +1,7 @@ -// vecs1.rs -// // Your task is to create a `Vec` which holds the exact same elements as in the // array `a`. // // Make me compile and pass the test! -// -// Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint. fn array_and_vec() -> ([i32; 4], Vec) { let a = [10, 20, 30, 40]; // a plain array diff --git a/exercises/05_vecs/vecs2.rs b/exercises/05_vecs/vecs2.rs index 1b16f0b..e72209c 100644 --- a/exercises/05_vecs/vecs2.rs +++ b/exercises/05_vecs/vecs2.rs @@ -1,11 +1,7 @@ -// vecs2.rs -// // A Vec of even numbers is given. Your task is to complete the loop so that // each number in the Vec is multiplied by 2. // // Make me pass the test! -// -// Execute `rustlings hint vecs2` or use the `hint` watch subcommand for a hint. fn vec_loop(mut v: Vec) -> Vec { for element in v.iter_mut() { diff --git a/exercises/06_move_semantics/move_semantics1.rs b/exercises/06_move_semantics/move_semantics1.rs index c612ba9..8c3fe3a 100644 --- a/exercises/06_move_semantics/move_semantics1.rs +++ b/exercises/06_move_semantics/move_semantics1.rs @@ -1,21 +1,25 @@ -// move_semantics1.rs -// -// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand -// for a hint. +fn fill_vec(vec: Vec) -> Vec { + let vec = vec; -#[test] -fn main() { - let vec0 = vec![22, 44, 66]; + vec.push(88); - let vec1 = fill_vec(vec0); + vec +} - assert_eq!(vec1, vec![22, 44, 66, 88]); +fn main() { + // You can optionally experiment here. } -fn fill_vec(vec: Vec) -> Vec { - let vec = vec; +#[cfg(test)] +mod tests { + use super::*; - vec.push(88); + #[test] + fn move_semantics1() { + let vec0 = vec![22, 44, 66]; - vec + let vec1 = fill_vec(vec0); + + assert_eq!(vec1, vec![22, 44, 66, 88]); + } } diff --git a/exercises/06_move_semantics/move_semantics2.rs b/exercises/06_move_semantics/move_semantics2.rs index 3457d11..d087911 100644 --- a/exercises/06_move_semantics/move_semantics2.rs +++ b/exercises/06_move_semantics/move_semantics2.rs @@ -1,19 +1,4 @@ -// move_semantics2.rs -// // 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. - -#[test] -fn main() { - let vec0 = vec![22, 44, 66]; - - let vec1 = fill_vec(vec0); - - 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; @@ -22,3 +7,22 @@ fn fill_vec(vec: Vec) -> Vec { vec } + +fn main() { + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn move_semantics2() { + let vec0 = vec![22, 44, 66]; + + let vec1 = fill_vec(vec0); + + assert_eq!(vec0, vec![22, 44, 66]); + assert_eq!(vec1, vec![22, 44, 66, 88]); + } +} diff --git a/exercises/06_move_semantics/move_semantics3.rs b/exercises/06_move_semantics/move_semantics3.rs index 9415eb1..24e3597 100644 --- a/exercises/06_move_semantics/move_semantics3.rs +++ b/exercises/06_move_semantics/move_semantics3.rs @@ -1,22 +1,26 @@ -// 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. -#[test] -fn main() { - let vec0 = vec![22, 44, 66]; +fn fill_vec(vec: Vec) -> Vec { + vec.push(88); - let vec1 = fill_vec(vec0); + vec +} - assert_eq!(vec1, vec![22, 44, 66, 88]); +fn main() { + // You can optionally experiment here. } -fn fill_vec(vec: Vec) -> Vec { - vec.push(88); +#[cfg(test)] +mod tests { + use super::*; - vec + #[test] + fn move_semantics3() { + let vec0 = vec![22, 44, 66]; + + let vec1 = fill_vec(vec0); + + assert_eq!(vec1, vec![22, 44, 66, 88]); + } } diff --git a/exercises/06_move_semantics/move_semantics4.rs b/exercises/06_move_semantics/move_semantics4.rs index 1509f5d..b662224 100644 --- a/exercises/06_move_semantics/move_semantics4.rs +++ b/exercises/06_move_semantics/move_semantics4.rs @@ -1,20 +1,6 @@ -// 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. - -#[test] -fn main() { - let vec0 = vec![22, 44, 66]; - - let vec1 = fill_vec(vec0); - - assert_eq!(vec1, vec![22, 44, 66, 88]); -} +// the test function. // `fill_vec()` no longer takes `vec: Vec` as argument - don't change this! fn fill_vec() -> Vec { @@ -25,3 +11,21 @@ fn fill_vec() -> Vec { vec } + +fn main() { + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn move_semantics4() { + let vec0 = vec![22, 44, 66]; + + let vec1 = fill_vec(vec0); + + assert_eq!(vec1, vec![22, 44, 66, 88]); + } +} diff --git a/exercises/06_move_semantics/move_semantics5.rs b/exercises/06_move_semantics/move_semantics5.rs index c84d2fe..b34560a 100644 --- a/exercises/06_move_semantics/move_semantics5.rs +++ b/exercises/06_move_semantics/move_semantics5.rs @@ -1,17 +1,21 @@ -// move_semantics5.rs -// -// Make me compile only by reordering the lines in `main()`, but without adding, +// Make me compile only by reordering the lines in the test, but without adding, // changing or removing any of them. -// -// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand -// for a hint. -#[test] fn main() { - let mut x = 100; - let y = &mut x; - let z = &mut x; - *y += 100; - *z += 1000; - assert_eq!(x, 1200); + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn move_semantics5() { + let mut x = 100; + let y = &mut x; + let z = &mut x; + *y += 100; + *z += 1000; + assert_eq!(x, 1200); + } } diff --git a/exercises/06_move_semantics/move_semantics6.rs b/exercises/06_move_semantics/move_semantics6.rs index 6059e61..2ad71db 100644 --- a/exercises/06_move_semantics/move_semantics6.rs +++ b/exercises/06_move_semantics/move_semantics6.rs @@ -1,9 +1,4 @@ -// move_semantics6.rs -// // You can't change anything except adding or removing references. -// -// Execute `rustlings hint move_semantics6` or use the `hint` watch subcommand -// for a hint. fn main() { let data = "Rust is great!".to_string(); diff --git a/exercises/07_structs/structs1.rs b/exercises/07_structs/structs1.rs index cd8b81c..62f1421 100644 --- a/exercises/07_structs/structs1.rs +++ b/exercises/07_structs/structs1.rs @@ -1,9 +1,4 @@ -// structs1.rs -// // Address all the TODOs to make the tests pass! -// -// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a -// hint. struct ColorClassicStruct { // TODO: Something goes here diff --git a/exercises/07_structs/structs2.rs b/exercises/07_structs/structs2.rs index 7e61e75..451dbe7 100644 --- a/exercises/07_structs/structs2.rs +++ b/exercises/07_structs/structs2.rs @@ -1,9 +1,4 @@ -// structs2.rs -// // Address all the TODOs to make the tests pass! -// -// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a -// hint. #[derive(Debug)] struct Order { diff --git a/exercises/07_structs/structs3.rs b/exercises/07_structs/structs3.rs index bd562a1..10adb48 100644 --- a/exercises/07_structs/structs3.rs +++ b/exercises/07_structs/structs3.rs @@ -1,11 +1,6 @@ -// structs3.rs -// // 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. // Make the code compile and the tests pass! -// -// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a -// hint. #[derive(Debug)] struct Package { diff --git a/exercises/08_enums/enums1.rs b/exercises/08_enums/enums1.rs index 330269c..d63de83 100644 --- a/exercises/08_enums/enums1.rs +++ b/exercises/08_enums/enums1.rs @@ -1,7 +1,3 @@ -// enums1.rs -// -// No hints this time! ;) - #[derive(Debug)] enum Message { // TODO: define a few types of messages as used below diff --git a/exercises/08_enums/enums2.rs b/exercises/08_enums/enums2.rs index f0e4e6d..f3b803f 100644 --- a/exercises/08_enums/enums2.rs +++ b/exercises/08_enums/enums2.rs @@ -1,8 +1,3 @@ -// enums2.rs -// -// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a -// hint. - #[derive(Debug)] enum Message { // TODO: define the different variants used below diff --git a/exercises/08_enums/enums3.rs b/exercises/08_enums/enums3.rs index 56c04fe..edac3df 100644 --- a/exercises/08_enums/enums3.rs +++ b/exercises/08_enums/enums3.rs @@ -1,9 +1,4 @@ -// enums3.rs -// // Address all the TODOs to make the tests pass! -// -// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a -// hint. enum Message { // TODO: implement the message variant types based on their usage below diff --git a/exercises/09_strings/strings1.rs b/exercises/09_strings/strings1.rs index a1255a3..de762eb 100644 --- a/exercises/09_strings/strings1.rs +++ b/exercises/09_strings/strings1.rs @@ -1,9 +1,4 @@ -// strings1.rs -// // Make me compile without changing the function signature! -// -// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a -// hint. fn main() { let answer = current_favorite_color(); diff --git a/exercises/09_strings/strings2.rs b/exercises/09_strings/strings2.rs index ba76fe6..4768278 100644 --- a/exercises/09_strings/strings2.rs +++ b/exercises/09_strings/strings2.rs @@ -1,9 +1,4 @@ -// strings2.rs -// // Make me compile without changing the function signature! -// -// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a -// hint. fn main() { let word = String::from("green"); // Try not changing this line :) diff --git a/exercises/09_strings/strings3.rs b/exercises/09_strings/strings3.rs index d53f654..f83a531 100644 --- a/exercises/09_strings/strings3.rs +++ b/exercises/09_strings/strings3.rs @@ -1,8 +1,3 @@ -// strings3.rs -// -// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a -// hint. - fn trim_me(input: &str) -> String { // TODO: Remove whitespace from both ends of a string! ??? diff --git a/exercises/09_strings/strings4.rs b/exercises/09_strings/strings4.rs index a034aa4..1f3d88b 100644 --- a/exercises/09_strings/strings4.rs +++ b/exercises/09_strings/strings4.rs @@ -1,11 +1,7 @@ -// strings4.rs -// -// Ok, here are a bunch of values-- some are `String`s, some are `&str`s. Your +// Ok, here are a bunch of values - some are `String`s, some are `&str`s. Your // task is to call one of these two functions on each value depending on what // you think each value is. That is, add either `string_slice` or `string` // before the parentheses on each line. If you're right, it will compile! -// -// No hints this time! fn string_slice(arg: &str) { println!("{}", arg); diff --git a/exercises/10_modules/modules1.rs b/exercises/10_modules/modules1.rs index c750946..931a3e2 100644 --- a/exercises/10_modules/modules1.rs +++ b/exercises/10_modules/modules1.rs @@ -1,8 +1,3 @@ -// modules1.rs -// -// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a -// hint. - mod sausage_factory { // Don't let anybody outside of this module see this! fn get_secret_recipe() -> String { diff --git a/exercises/10_modules/modules2.rs b/exercises/10_modules/modules2.rs index 4d3106c..5f8b0d5 100644 --- a/exercises/10_modules/modules2.rs +++ b/exercises/10_modules/modules2.rs @@ -1,11 +1,6 @@ -// 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. mod delicious_snacks { // TODO: Fix these use statements diff --git a/exercises/10_modules/modules3.rs b/exercises/10_modules/modules3.rs index c211a76..eff24a9 100644 --- a/exercises/10_modules/modules3.rs +++ b/exercises/10_modules/modules3.rs @@ -1,12 +1,7 @@ -// 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. // TODO: Complete this use statement use ??? diff --git a/exercises/11_hashmaps/hashmaps1.rs b/exercises/11_hashmaps/hashmaps1.rs index 51146df..e646ed7 100644 --- a/exercises/11_hashmaps/hashmaps1.rs +++ b/exercises/11_hashmaps/hashmaps1.rs @@ -1,5 +1,3 @@ -// 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 @@ -7,9 +5,6 @@ // 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. use std::collections::HashMap; diff --git a/exercises/11_hashmaps/hashmaps2.rs b/exercises/11_hashmaps/hashmaps2.rs index 47983f6..e6380d9 100644 --- a/exercises/11_hashmaps/hashmaps2.rs +++ b/exercises/11_hashmaps/hashmaps2.rs @@ -1,5 +1,3 @@ -// 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 @@ -10,9 +8,6 @@ // 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. use std::collections::HashMap; diff --git a/exercises/11_hashmaps/hashmaps3.rs b/exercises/11_hashmaps/hashmaps3.rs index 3322909..070c370 100644 --- a/exercises/11_hashmaps/hashmaps3.rs +++ b/exercises/11_hashmaps/hashmaps3.rs @@ -1,5 +1,3 @@ -// hashmaps3.rs -// // 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). @@ -11,9 +9,6 @@ // complete it to pass the test. // // Make me pass the tests! -// -// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a -// hint. use std::collections::HashMap; diff --git a/exercises/12_options/options1.rs b/exercises/12_options/options1.rs index aecb123..b7cf7b0 100644 --- a/exercises/12_options/options1.rs +++ b/exercises/12_options/options1.rs @@ -1,8 +1,3 @@ -// options1.rs -// -// Execute `rustlings hint options1` or use the `hint` watch subcommand for a -// hint. - // This function returns how much icecream there is left in the fridge. // If it's before 10PM, there's 5 scoops left. At 10PM, someone eats it // all, so there'll be no more left :( diff --git a/exercises/12_options/options2.rs b/exercises/12_options/options2.rs index d183d1d..01f84c5 100644 --- a/exercises/12_options/options2.rs +++ b/exercises/12_options/options2.rs @@ -1,8 +1,3 @@ -// options2.rs -// -// Execute `rustlings hint options2` or use the `hint` watch subcommand for a -// hint. - fn main() { // You can optionally experiment here. } diff --git a/exercises/12_options/options3.rs b/exercises/12_options/options3.rs index 7922ef9..5b70a79 100644 --- a/exercises/12_options/options3.rs +++ b/exercises/12_options/options3.rs @@ -1,8 +1,3 @@ -// options3.rs -// -// Execute `rustlings hint options3` or use the `hint` watch subcommand for a -// hint. - struct Point { x: i32, y: i32, diff --git a/exercises/13_error_handling/errors1.rs b/exercises/13_error_handling/errors1.rs index 7991c42..15a3716 100644 --- a/exercises/13_error_handling/errors1.rs +++ b/exercises/13_error_handling/errors1.rs @@ -1,13 +1,8 @@ -// 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. fn main() { // You can optionally experiment here. diff --git a/exercises/13_error_handling/errors2.rs b/exercises/13_error_handling/errors2.rs index 051516b..e39aa95 100644 --- a/exercises/13_error_handling/errors2.rs +++ b/exercises/13_error_handling/errors2.rs @@ -1,5 +1,3 @@ -// 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 @@ -15,9 +13,6 @@ // // 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. use std::num::ParseIntError; diff --git a/exercises/13_error_handling/errors3.rs b/exercises/13_error_handling/errors3.rs index 56bb31b..5661f17 100644 --- a/exercises/13_error_handling/errors3.rs +++ b/exercises/13_error_handling/errors3.rs @@ -1,11 +1,6 @@ -// 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. use std::num::ParseIntError; diff --git a/exercises/13_error_handling/errors4.rs b/exercises/13_error_handling/errors4.rs index 9449417..993d42a 100644 --- a/exercises/13_error_handling/errors4.rs +++ b/exercises/13_error_handling/errors4.rs @@ -1,8 +1,3 @@ -// errors4.rs -// -// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a -// hint. - #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); @@ -23,12 +18,17 @@ fn main() { // You can optionally experiment here. } -#[test] -fn test_creation() { - assert!(PositiveNonzeroInteger::new(10).is_ok()); - assert_eq!( - Err(CreationError::Negative), - PositiveNonzeroInteger::new(-10) - ); - assert_eq!(Err(CreationError::Zero), PositiveNonzeroInteger::new(0)); +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_creation() { + assert!(PositiveNonzeroInteger::new(10).is_ok()); + assert_eq!( + Err(CreationError::Negative), + PositiveNonzeroInteger::new(-10) + ); + assert_eq!(Err(CreationError::Zero), PositiveNonzeroInteger::new(0)); + } } diff --git a/exercises/13_error_handling/errors5.rs b/exercises/13_error_handling/errors5.rs index 0bcb4b8..7192562 100644 --- a/exercises/13_error_handling/errors5.rs +++ b/exercises/13_error_handling/errors5.rs @@ -1,5 +1,3 @@ -// 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 @@ -18,9 +16,6 @@ // // 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. use std::error; use std::fmt; diff --git a/exercises/13_error_handling/errors6.rs b/exercises/13_error_handling/errors6.rs index 363a3b9..8b08e08 100644 --- a/exercises/13_error_handling/errors6.rs +++ b/exercises/13_error_handling/errors6.rs @@ -1,13 +1,8 @@ -// 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. use std::num::ParseIntError; diff --git a/exercises/14_generics/generics1.rs b/exercises/14_generics/generics1.rs index 545fd95..c023e64 100644 --- a/exercises/14_generics/generics1.rs +++ b/exercises/14_generics/generics1.rs @@ -1,10 +1,5 @@ -// 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. fn main() { let mut shopping_list: Vec = Vec::new(); diff --git a/exercises/14_generics/generics2.rs b/exercises/14_generics/generics2.rs index 068468b..cbb9b5f 100644 --- a/exercises/14_generics/generics2.rs +++ b/exercises/14_generics/generics2.rs @@ -1,10 +1,5 @@ -// 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. struct Wrapper { value: u32, diff --git a/exercises/15_traits/traits1.rs b/exercises/15_traits/traits1.rs index c51d3b8..b17c9c6 100644 --- a/exercises/15_traits/traits1.rs +++ b/exercises/15_traits/traits1.rs @@ -1,11 +1,6 @@ -// traits1.rs -// // Time to implement some traits! Your task is to implement the trait // `AppendBar` for the type `String`. The trait AppendBar has only one function, // which appends "Bar" to any object implementing this trait. -// -// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a -// hint. trait AppendBar { fn append_bar(self) -> Self; diff --git a/exercises/15_traits/traits2.rs b/exercises/15_traits/traits2.rs index 18ebcb0..170779b 100644 --- a/exercises/15_traits/traits2.rs +++ b/exercises/15_traits/traits2.rs @@ -1,12 +1,8 @@ -// traits2.rs -// // Your task is to implement the trait `AppendBar` for a vector of strings. To // implement this trait, consider for a moment what it means to 'append "Bar"' // to a vector of strings. // // No boiler plate code this time, you can do this! -// -// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint. trait AppendBar { fn append_bar(self) -> Self; diff --git a/exercises/15_traits/traits3.rs b/exercises/15_traits/traits3.rs index 8412afa..9a2365a 100644 --- a/exercises/15_traits/traits3.rs +++ b/exercises/15_traits/traits3.rs @@ -1,12 +1,7 @@ -// traits3.rs -// // Your task is to implement the Licensed trait for both structures and have // them return the same information without writing the same function twice. // // Consider what you can add to the Licensed trait. -// -// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a -// hint. pub trait Licensed { fn licensing_info(&self) -> String; diff --git a/exercises/15_traits/traits4.rs b/exercises/15_traits/traits4.rs index 18db0d6..7af30b5 100644 --- a/exercises/15_traits/traits4.rs +++ b/exercises/15_traits/traits4.rs @@ -1,11 +1,6 @@ -// traits4.rs -// // Your task is to replace the '??' sections so the code compiles. // // Don't change any line other than the marked one. -// -// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a -// hint. pub trait Licensed { fn licensing_info(&self) -> String { diff --git a/exercises/15_traits/traits5.rs b/exercises/15_traits/traits5.rs index f258d32..9a45bb7 100644 --- a/exercises/15_traits/traits5.rs +++ b/exercises/15_traits/traits5.rs @@ -1,11 +1,6 @@ -// traits5.rs -// // Your task is to replace the '??' sections so the code compiles. // // Don't change any line other than the marked one. -// -// Execute `rustlings hint traits5` or use the `hint` watch subcommand for a -// hint. pub trait SomeTrait { fn some_function(&self) -> bool { diff --git a/exercises/16_lifetimes/lifetimes1.rs b/exercises/16_lifetimes/lifetimes1.rs index 4f544b4..d34f3ab 100644 --- a/exercises/16_lifetimes/lifetimes1.rs +++ b/exercises/16_lifetimes/lifetimes1.rs @@ -1,12 +1,7 @@ -// 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? -// -// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a -// hint. fn longest(x: &str, y: &str) -> &str { if x.len() > y.len() { diff --git a/exercises/16_lifetimes/lifetimes2.rs b/exercises/16_lifetimes/lifetimes2.rs index 33b5565..6e329e6 100644 --- a/exercises/16_lifetimes/lifetimes2.rs +++ b/exercises/16_lifetimes/lifetimes2.rs @@ -1,10 +1,5 @@ -// 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? -// -// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a -// hint. fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { if x.len() > y.len() { diff --git a/exercises/16_lifetimes/lifetimes3.rs b/exercises/16_lifetimes/lifetimes3.rs index de6005e..9b631ca 100644 --- a/exercises/16_lifetimes/lifetimes3.rs +++ b/exercises/16_lifetimes/lifetimes3.rs @@ -1,9 +1,4 @@ -// lifetimes3.rs -// // Lifetimes are also needed when structs hold references. -// -// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a -// hint. struct Book { author: &str, @@ -13,7 +8,10 @@ struct Book { fn main() { let name = String::from("Jill Smith"); let title = String::from("Fish Flying"); - let book = Book { author: &name, title: &title }; + let book = Book { + author: &name, + title: &title, + }; println!("{} by {}", book.title, book.author); } diff --git a/exercises/17_tests/tests1.rs b/exercises/17_tests/tests1.rs index d32ace1..854a135 100644 --- a/exercises/17_tests/tests1.rs +++ b/exercises/17_tests/tests1.rs @@ -1,14 +1,9 @@ -// tests1.rs -// // Tests are important to ensure that your code does what you think it should // do. Tests can be run on this file with the following command: rustlings run // tests1 // // This test has a problem with it -- make the test compile! Make the test pass! // Make the test fail! -// -// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a -// hint. fn main() { // You can optionally experiment here. diff --git a/exercises/17_tests/tests2.rs b/exercises/17_tests/tests2.rs index 501c44b..f0899e1 100644 --- a/exercises/17_tests/tests2.rs +++ b/exercises/17_tests/tests2.rs @@ -1,10 +1,5 @@ -// tests2.rs -// // This test has a problem with it -- make the test compile! Make the test pass! // Make the test fail! -// -// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a -// hint. fn main() { // You can optionally experiment here. diff --git a/exercises/17_tests/tests3.rs b/exercises/17_tests/tests3.rs index a2093cf..3b4e199 100644 --- a/exercises/17_tests/tests3.rs +++ b/exercises/17_tests/tests3.rs @@ -1,11 +1,6 @@ -// tests3.rs -// // This test isn't testing our function -- make it do that in such a way that // the test passes. Then write a second test that tests whether we get the // result we expect to get when we call `is_even(5)`. -// -// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a -// hint. pub fn is_even(num: i32) -> bool { num % 2 == 0 diff --git a/exercises/17_tests/tests4.rs b/exercises/17_tests/tests4.rs index a50323c..35a9a3b 100644 --- a/exercises/17_tests/tests4.rs +++ b/exercises/17_tests/tests4.rs @@ -1,9 +1,4 @@ -// tests4.rs -// // Make sure that we're testing for the correct conditions! -// -// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a -// hint. struct Rectangle { width: i32, diff --git a/exercises/18_iterators/iterators1.rs b/exercises/18_iterators/iterators1.rs index 7ec7da2..52b704d 100644 --- a/exercises/18_iterators/iterators1.rs +++ b/exercises/18_iterators/iterators1.rs @@ -1,24 +1,28 @@ -// iterators1.rs -// // 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. -#[test] fn main() { - let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"]; + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn iterators() { + let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"]; - let mut my_iterable_fav_fruits = ???; // TODO: Step 1 + let mut my_iterable_fav_fruits = ???; // TODO: Step 1 - assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2 - assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3 - assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry")); - assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4 + assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana")); + assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2 + assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado")); + assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3 + assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry")); + assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4 + } } diff --git a/exercises/18_iterators/iterators2.rs b/exercises/18_iterators/iterators2.rs index 0ebd69a..df1fa83 100644 --- a/exercises/18_iterators/iterators2.rs +++ b/exercises/18_iterators/iterators2.rs @@ -1,10 +1,5 @@ -// 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. // Step 1. // Complete the `capitalize_first` function. diff --git a/exercises/18_iterators/iterators3.rs b/exercises/18_iterators/iterators3.rs index 3f5923c..9f106aa 100644 --- a/exercises/18_iterators/iterators3.rs +++ b/exercises/18_iterators/iterators3.rs @@ -1,13 +1,8 @@ -// 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: // 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. #[derive(Debug, PartialEq, Eq)] pub enum DivisionError { diff --git a/exercises/18_iterators/iterators4.rs b/exercises/18_iterators/iterators4.rs index 8fc8792..60c7b8d 100644 --- a/exercises/18_iterators/iterators4.rs +++ b/exercises/18_iterators/iterators4.rs @@ -1,8 +1,3 @@ -// iterators4.rs -// -// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a -// hint. - pub fn factorial(num: u64) -> u64 { // Complete this function to return the factorial of num // Do not use: @@ -12,7 +7,6 @@ pub fn factorial(num: u64) -> u64 { // - additional variables // For an extra challenge, don't use: // - recursion - // Execute `rustlings hint iterators4` for hints. } fn main() { diff --git a/exercises/18_iterators/iterators5.rs b/exercises/18_iterators/iterators5.rs index 2604004..4f052d5 100644 --- a/exercises/18_iterators/iterators5.rs +++ b/exercises/18_iterators/iterators5.rs @@ -1,5 +1,3 @@ -// 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 @@ -7,9 +5,6 @@ // 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. use std::collections::HashMap; diff --git a/exercises/19_smart_pointers/arc1.rs b/exercises/19_smart_pointers/arc1.rs index 0647eea..7b31fa8 100644 --- a/exercises/19_smart_pointers/arc1.rs +++ b/exercises/19_smart_pointers/arc1.rs @@ -1,5 +1,3 @@ -// arc1.rs -// // In this exercise, we are given a Vec of u32 called "numbers" with values // ranging from 0 to 99 -- [ 0, 1, 2, ..., 98, 99 ] We would like to use this // set of numbers within 8 different threads simultaneously. Each thread is @@ -18,8 +16,6 @@ // first TODO comment is, and create an initial binding for `child_numbers` // where the second TODO comment is. Try not to create any copies of the // `numbers` Vec! -// -// Execute `rustlings hint arc1` or use the `hint` watch subcommand for a hint. #![forbid(unused_imports)] // Do not change this, (or the next) line. use std::sync::Arc; diff --git a/exercises/19_smart_pointers/box1.rs b/exercises/19_smart_pointers/box1.rs index 2abc024..226a117 100644 --- a/exercises/19_smart_pointers/box1.rs +++ b/exercises/19_smart_pointers/box1.rs @@ -1,5 +1,3 @@ -// box1.rs -// // At compile time, Rust needs to know how much space a type takes up. This // becomes problematic for recursive types, where a value can have as part of // itself another value of the same type. To get around the issue, we can use a @@ -15,8 +13,6 @@ // Step 2: create both empty and non-empty cons lists by replacing `todo!()` // // Note: the tests should not be changed -// -// Execute `rustlings hint box1` or use the `hint` watch subcommand for a hint. #[derive(PartialEq, Debug)] pub enum List { diff --git a/exercises/19_smart_pointers/cow1.rs b/exercises/19_smart_pointers/cow1.rs index 51e5fdb..754c0ba 100644 --- a/exercises/19_smart_pointers/cow1.rs +++ b/exercises/19_smart_pointers/cow1.rs @@ -1,5 +1,3 @@ -// cow1.rs -// // This exercise explores the Cow, or Clone-On-Write type. Cow is a // clone-on-write smart pointer. It can enclose and provide immutable access to // borrowed data, and clone the data lazily when mutation or ownership is @@ -9,8 +7,6 @@ // This exercise is meant to show you what to expect when passing data to Cow. // Fix the unit tests by checking for Cow::Owned(_) and Cow::Borrowed(_) at the // TODO markers. -// -// Execute `rustlings hint cow1` or use the `hint` watch subcommand for a hint. use std::borrow::Cow; diff --git a/exercises/19_smart_pointers/rc1.rs b/exercises/19_smart_pointers/rc1.rs index e96e625..19de3db 100644 --- a/exercises/19_smart_pointers/rc1.rs +++ b/exercises/19_smart_pointers/rc1.rs @@ -1,5 +1,3 @@ -// rc1.rs -// // In this exercise, we want to express the concept of multiple owners via the // Rc type. This is a model of our solar system - there is a Sun type and // multiple Planets. The Planets take ownership of the sun, indicating that they @@ -7,8 +5,6 @@ // // Make this code compile by using the proper Rc primitives to express that the // sun has multiple owners. -// -// Execute `rustlings hint rc1` or use the `hint` watch subcommand for a hint. use std::rc::Rc; @@ -33,71 +29,80 @@ impl Planet { } } -#[test] fn main() { - let sun = Rc::new(Sun {}); - println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference + // You can optionally experiment here. +} - let mercury = Planet::Mercury(Rc::clone(&sun)); - println!("reference count = {}", Rc::strong_count(&sun)); // 2 references - mercury.details(); +#[cfg(test)] +mod tests { + use super::*; - let venus = Planet::Venus(Rc::clone(&sun)); - println!("reference count = {}", Rc::strong_count(&sun)); // 3 references - venus.details(); + #[test] + fn rc1() { + let sun = Rc::new(Sun {}); + println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference - let earth = Planet::Earth(Rc::clone(&sun)); - println!("reference count = {}", Rc::strong_count(&sun)); // 4 references - earth.details(); + let mercury = Planet::Mercury(Rc::clone(&sun)); + println!("reference count = {}", Rc::strong_count(&sun)); // 2 references + mercury.details(); - let mars = Planet::Mars(Rc::clone(&sun)); - println!("reference count = {}", Rc::strong_count(&sun)); // 5 references - mars.details(); + let venus = Planet::Venus(Rc::clone(&sun)); + println!("reference count = {}", Rc::strong_count(&sun)); // 3 references + venus.details(); - let jupiter = Planet::Jupiter(Rc::clone(&sun)); - println!("reference count = {}", Rc::strong_count(&sun)); // 6 references - jupiter.details(); + let earth = Planet::Earth(Rc::clone(&sun)); + println!("reference count = {}", Rc::strong_count(&sun)); // 4 references + earth.details(); - // TODO - let saturn = Planet::Saturn(Rc::new(Sun {})); - println!("reference count = {}", Rc::strong_count(&sun)); // 7 references - saturn.details(); + let mars = Planet::Mars(Rc::clone(&sun)); + println!("reference count = {}", Rc::strong_count(&sun)); // 5 references + mars.details(); - // TODO - let uranus = Planet::Uranus(Rc::new(Sun {})); - println!("reference count = {}", Rc::strong_count(&sun)); // 8 references - uranus.details(); + let jupiter = Planet::Jupiter(Rc::clone(&sun)); + println!("reference count = {}", Rc::strong_count(&sun)); // 6 references + jupiter.details(); - // TODO - let neptune = Planet::Neptune(Rc::new(Sun {})); - println!("reference count = {}", Rc::strong_count(&sun)); // 9 references - neptune.details(); + // TODO + let saturn = Planet::Saturn(Rc::new(Sun {})); + println!("reference count = {}", Rc::strong_count(&sun)); // 7 references + saturn.details(); - assert_eq!(Rc::strong_count(&sun), 9); + // TODO + let uranus = Planet::Uranus(Rc::new(Sun {})); + println!("reference count = {}", Rc::strong_count(&sun)); // 8 references + uranus.details(); - drop(neptune); - println!("reference count = {}", Rc::strong_count(&sun)); // 8 references + // TODO + let neptune = Planet::Neptune(Rc::new(Sun {})); + println!("reference count = {}", Rc::strong_count(&sun)); // 9 references + neptune.details(); - drop(uranus); - println!("reference count = {}", Rc::strong_count(&sun)); // 7 references + assert_eq!(Rc::strong_count(&sun), 9); - drop(saturn); - println!("reference count = {}", Rc::strong_count(&sun)); // 6 references + drop(neptune); + println!("reference count = {}", Rc::strong_count(&sun)); // 8 references - drop(jupiter); - println!("reference count = {}", Rc::strong_count(&sun)); // 5 references + drop(uranus); + println!("reference count = {}", Rc::strong_count(&sun)); // 7 references - drop(mars); - println!("reference count = {}", Rc::strong_count(&sun)); // 4 references + drop(saturn); + println!("reference count = {}", Rc::strong_count(&sun)); // 6 references - // TODO - println!("reference count = {}", Rc::strong_count(&sun)); // 3 references + drop(jupiter); + println!("reference count = {}", Rc::strong_count(&sun)); // 5 references - // TODO - println!("reference count = {}", Rc::strong_count(&sun)); // 2 references + drop(mars); + println!("reference count = {}", Rc::strong_count(&sun)); // 4 references - // TODO - println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference + // TODO + println!("reference count = {}", Rc::strong_count(&sun)); // 3 references - assert_eq!(Rc::strong_count(&sun), 1); + // TODO + println!("reference count = {}", Rc::strong_count(&sun)); // 2 references + + // TODO + println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference + + assert_eq!(Rc::strong_count(&sun), 1); + } } diff --git a/exercises/20_threads/threads1.rs b/exercises/20_threads/threads1.rs index be1301d..bf0b8e0 100644 --- a/exercises/20_threads/threads1.rs +++ b/exercises/20_threads/threads1.rs @@ -1,12 +1,7 @@ -// threads1.rs -// // This program spawns multiple threads that each run for at least 250ms, and // each thread returns how much time they took to complete. The program should // wait until all the spawned threads have finished and should collect their // return values into a vector. -// -// Execute `rustlings hint threads1` or use the `hint` watch subcommand for a -// hint. use std::thread; use std::time::{Duration, Instant}; diff --git a/exercises/20_threads/threads2.rs b/exercises/20_threads/threads2.rs index 13cb840..2bdeba9 100644 --- a/exercises/20_threads/threads2.rs +++ b/exercises/20_threads/threads2.rs @@ -1,11 +1,6 @@ -// threads2.rs -// // Building on the last exercise, we want all of the threads to complete their // work but this time the spawned threads need to be in charge of updating a // shared value: JobStatus.jobs_completed -// -// Execute `rustlings hint threads2` or use the `hint` watch subcommand for a -// hint. use std::sync::Arc; use std::thread; diff --git a/exercises/20_threads/threads3.rs b/exercises/20_threads/threads3.rs index 35b914a..13abc45 100644 --- a/exercises/20_threads/threads3.rs +++ b/exercises/20_threads/threads3.rs @@ -1,8 +1,3 @@ -// threads3.rs -// -// Execute `rustlings hint threads3` or use the `hint` watch subcommand for a -// hint. - use std::sync::mpsc; use std::sync::Arc; use std::thread; @@ -42,20 +37,29 @@ fn send_tx(q: Queue, tx: mpsc::Sender) -> () { }); } -#[test] fn main() { - let (tx, rx) = mpsc::channel(); - let queue = Queue::new(); - let queue_length = queue.length; + // You can optionally experiment here. +} - send_tx(queue, tx); +#[cfg(test)] +mod tests { + use super::*; - let mut total_received: u32 = 0; - for received in rx { - println!("Got: {}", received); - total_received += 1; - } + #[test] + fn threads3() { + let (tx, rx) = mpsc::channel(); + let queue = Queue::new(); + let queue_length = queue.length; + + send_tx(queue, tx); - println!("total numbers received: {}", total_received); - assert_eq!(total_received, queue_length) + let mut total_received: u32 = 0; + for received in rx { + println!("Got: {}", received); + total_received += 1; + } + + println!("total numbers received: {}", total_received); + assert_eq!(total_received, queue_length) + } } diff --git a/exercises/21_macros/macros1.rs b/exercises/21_macros/macros1.rs index 65986db..1d415cb 100644 --- a/exercises/21_macros/macros1.rs +++ b/exercises/21_macros/macros1.rs @@ -1,8 +1,3 @@ -// macros1.rs -// -// Execute `rustlings hint macros1` or use the `hint` watch subcommand for a -// hint. - macro_rules! my_macro { () => { println!("Check out my macro!"); diff --git a/exercises/21_macros/macros2.rs b/exercises/21_macros/macros2.rs index b7c37fd..f16712b 100644 --- a/exercises/21_macros/macros2.rs +++ b/exercises/21_macros/macros2.rs @@ -1,8 +1,3 @@ -// macros2.rs -// -// Execute `rustlings hint macros2` or use the `hint` watch subcommand for a -// hint. - fn main() { my_macro!(); } diff --git a/exercises/21_macros/macros3.rs b/exercises/21_macros/macros3.rs index 92a1922..405c397 100644 --- a/exercises/21_macros/macros3.rs +++ b/exercises/21_macros/macros3.rs @@ -1,9 +1,4 @@ -// 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. mod macros { macro_rules! my_macro { diff --git a/exercises/21_macros/macros4.rs b/exercises/21_macros/macros4.rs index 83a6e44..03ece08 100644 --- a/exercises/21_macros/macros4.rs +++ b/exercises/21_macros/macros4.rs @@ -1,8 +1,3 @@ -// macros4.rs -// -// Execute `rustlings hint macros4` or use the `hint` watch subcommand for a -// hint. - #[rustfmt::skip] macro_rules! my_macro { () => { diff --git a/exercises/22_clippy/clippy1.rs b/exercises/22_clippy/clippy1.rs index 1e0f42e..f1eaa83 100644 --- a/exercises/22_clippy/clippy1.rs +++ b/exercises/22_clippy/clippy1.rs @@ -1,13 +1,8 @@ -// 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. use std::f32; diff --git a/exercises/22_clippy/clippy2.rs b/exercises/22_clippy/clippy2.rs index 37ac089..c7d400d 100644 --- a/exercises/22_clippy/clippy2.rs +++ b/exercises/22_clippy/clippy2.rs @@ -1,8 +1,3 @@ -// clippy2.rs -// -// Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a -// hint. - fn main() { let mut res = 42; let option = Some(12); diff --git a/exercises/22_clippy/clippy3.rs b/exercises/22_clippy/clippy3.rs index 6a6a36b..fd829cf 100644 --- a/exercises/22_clippy/clippy3.rs +++ b/exercises/22_clippy/clippy3.rs @@ -1,7 +1,4 @@ -// clippy3.rs -// // Here's a couple more easy Clippy fixes, so you can see its utility. -// No hints. #[allow(unused_variables, unused_assignments)] fn main() { diff --git a/exercises/23_conversions/as_ref_mut.rs b/exercises/23_conversions/as_ref_mut.rs index 6fb7c2f..c725dfd 100644 --- a/exercises/23_conversions/as_ref_mut.rs +++ b/exercises/23_conversions/as_ref_mut.rs @@ -1,11 +1,6 @@ -// 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. // Obtain the number of bytes (not characters) in the given argument. // TODO: Add the AsRef trait appropriately as a trait bound. diff --git a/exercises/23_conversions/from_into.rs b/exercises/23_conversions/from_into.rs index d2a1609..9df10da 100644 --- a/exercises/23_conversions/from_into.rs +++ b/exercises/23_conversions/from_into.rs @@ -1,11 +1,6 @@ -// 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 { @@ -24,7 +19,6 @@ impl Default for Person { } } - // Your task is to complete this implementation in order for the line `let p1 = // 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 diff --git a/exercises/23_conversions/from_str.rs b/exercises/23_conversions/from_str.rs index ed91ca5..58270f0 100644 --- a/exercises/23_conversions/from_str.rs +++ b/exercises/23_conversions/from_str.rs @@ -1,13 +1,8 @@ -// 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. use std::num::ParseIntError; use std::str::FromStr; diff --git a/exercises/23_conversions/try_from_into.rs b/exercises/23_conversions/try_from_into.rs index 2316655..da45e5a 100644 --- a/exercises/23_conversions/try_from_into.rs +++ b/exercises/23_conversions/try_from_into.rs @@ -1,13 +1,8 @@ -// 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. use std::convert::{TryFrom, TryInto}; diff --git a/exercises/23_conversions/using_as.rs b/exercises/23_conversions/using_as.rs index 9f617ec..94b1bb3 100644 --- a/exercises/23_conversions/using_as.rs +++ b/exercises/23_conversions/using_as.rs @@ -1,14 +1,9 @@ -// using_as.rs -// // 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. fn average(values: &[f64]) -> f64 { let total = values.iter().sum::(); diff --git a/exercises/quiz1.rs b/exercises/quiz1.rs index 55bc61f..edb672e 100644 --- a/exercises/quiz1.rs +++ b/exercises/quiz1.rs @@ -1,5 +1,3 @@ -// quiz1.rs -// // This is a quiz for the following sections: // - Variables // - Functions @@ -10,8 +8,6 @@ // - 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. -// -// No hints this time ;) // Put your function here! // fn calculate_price_of_apples { @@ -20,16 +16,21 @@ fn main() { // You can optionally experiment here. } -// Don't modify this function! -#[test] -fn verify_test() { - let price1 = calculate_price_of_apples(35); - let price2 = calculate_price_of_apples(40); - let price3 = calculate_price_of_apples(41); - let price4 = calculate_price_of_apples(65); +#[cfg(test)] +mod tests { + use super::*; + + // Don't modify this test! + #[test] + fn verify_test() { + let price1 = calculate_price_of_apples(35); + let price2 = calculate_price_of_apples(40); + let price3 = calculate_price_of_apples(41); + let price4 = calculate_price_of_apples(65); - assert_eq!(70, price1); - assert_eq!(80, price2); - assert_eq!(41, price3); - assert_eq!(65, price4); + assert_eq!(70, price1); + assert_eq!(80, price2); + assert_eq!(41, price3); + assert_eq!(65, price4); + } } diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 1d73ab9..0a29e78 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -1,5 +1,3 @@ -// quiz2.rs -// // This is a quiz for the following sections: // - Strings // - Vecs @@ -17,8 +15,6 @@ // - The input is going to be a Vector of a 2-length tuple, // the first element is the string, the second one is the command. // - The output element is going to be a Vector of strings. -// -// No hints this time! pub enum Command { Uppercase, diff --git a/exercises/quiz3.rs b/exercises/quiz3.rs index 780e130..f255cb5 100644 --- a/exercises/quiz3.rs +++ b/exercises/quiz3.rs @@ -1,5 +1,3 @@ -// quiz3.rs -// // This quiz tests: // - Generics // - Traits @@ -13,8 +11,6 @@ // Make the necessary code changes in the struct ReportCard and the impl block // to support alphabetical report cards. Change the Grade in the second test to // "A+" to show that your changes allow alphabetical grades. -// -// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint. pub struct ReportCard { pub grade: f32, -- cgit v1.2.3 From f53d4589205a5485011a341400eeea0ec3d6b339 Mon Sep 17 00:00:00 2001 From: mo8it Date: Fri, 28 Jun 2024 16:11:14 +0200 Subject: iterators5 solution --- exercises/18_iterators/iterators5.rs | 104 ++++++++++++------------ solutions/18_iterators/iterators5.rs | 151 ++++++++++++++++++++++++++++++++++- 2 files changed, 202 insertions(+), 53 deletions(-) (limited to 'exercises/18_iterators/iterators5.rs') diff --git a/exercises/18_iterators/iterators5.rs b/exercises/18_iterators/iterators5.rs index 4f052d5..7e434cc 100644 --- a/exercises/18_iterators/iterators5.rs +++ b/exercises/18_iterators/iterators5.rs @@ -1,10 +1,8 @@ -// Let's define a simple model to track Rustlings exercise progress. Progress +// 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 // number of exercises with a given progress. Recreate this counting -// 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. +// functionality using iterators. Try to not use imperative loops (for/while). use std::collections::HashMap; @@ -18,24 +16,25 @@ enum Progress { fn count_for(map: &HashMap, value: Progress) -> usize { let mut count = 0; for val in map.values() { - if val == &value { + if *val == value { count += 1; } } count } +// TODO: Implement the functionality of `count_for` but with an iterator instead +// of a `for` loop. fn count_iterator(map: &HashMap, value: Progress) -> usize { - // map is a hashmap with String keys and Progress values. - // map = { "variables1": Complete, "from_str": None, ... } - todo!(); + // `map` is a hash map with `String` keys and `Progress` values. + // map = { "variables1": Complete, "from_str": None, … } } fn count_collection_for(collection: &[HashMap], value: Progress) -> usize { let mut count = 0; for map in collection { for val in map.values() { - if val == &value { + if *val == value { count += 1; } } @@ -43,11 +42,12 @@ fn count_collection_for(collection: &[HashMap], value: Progres count } +// TODO: Implement the functionality of `count_collection_for` but with an +// iterator instead of a `for` loop. fn count_collection_iterator(collection: &[HashMap], value: Progress) -> usize { - // collection is a slice of hashmaps. - // collection = [{ "variables1": Complete, "from_str": None, ... }, - // { "variables2": Complete, ... }, ... ] - todo!(); + // `collection` is a slice of hash maps. + // collection = [{ "variables1": Complete, "from_str": None, … }, + // { "variables2": Complete, … }, … ] } fn main() { @@ -58,32 +58,61 @@ fn main() { mod tests { use super::*; + fn get_map() -> HashMap { + use Progress::*; + + let mut map = HashMap::new(); + map.insert(String::from("variables1"), Complete); + map.insert(String::from("functions1"), Complete); + map.insert(String::from("hashmap1"), Complete); + map.insert(String::from("arc1"), Some); + map.insert(String::from("as_ref_mut"), None); + map.insert(String::from("from_str"), None); + + map + } + + fn get_vec_map() -> Vec> { + use Progress::*; + + let map = get_map(); + + let mut other = HashMap::new(); + other.insert(String::from("variables2"), Complete); + other.insert(String::from("functions2"), Complete); + other.insert(String::from("if1"), Complete); + other.insert(String::from("from_into"), None); + other.insert(String::from("try_from_into"), None); + + vec![map, other] + } + #[test] fn count_complete() { let map = get_map(); - assert_eq!(3, count_iterator(&map, Progress::Complete)); + assert_eq!(count_iterator(&map, Progress::Complete), 3); } #[test] fn count_some() { let map = get_map(); - assert_eq!(1, count_iterator(&map, Progress::Some)); + assert_eq!(count_iterator(&map, Progress::Some), 1); } #[test] fn count_none() { let map = get_map(); - assert_eq!(2, count_iterator(&map, Progress::None)); + assert_eq!(count_iterator(&map, Progress::None), 2); } #[test] fn count_complete_equals_for() { let map = get_map(); - let progress_states = vec![Progress::Complete, Progress::Some, Progress::None]; + let progress_states = [Progress::Complete, Progress::Some, Progress::None]; for progress_state in progress_states { assert_eq!( count_for(&map, progress_state), - count_iterator(&map, progress_state) + count_iterator(&map, progress_state), ); } } @@ -92,62 +121,33 @@ mod tests { fn count_collection_complete() { let collection = get_vec_map(); assert_eq!( + count_collection_iterator(&collection, Progress::Complete), 6, - count_collection_iterator(&collection, Progress::Complete) ); } #[test] fn count_collection_some() { let collection = get_vec_map(); - assert_eq!(1, count_collection_iterator(&collection, Progress::Some)); + assert_eq!(count_collection_iterator(&collection, Progress::Some), 1); } #[test] fn count_collection_none() { let collection = get_vec_map(); - assert_eq!(4, count_collection_iterator(&collection, Progress::None)); + assert_eq!(count_collection_iterator(&collection, Progress::None), 4); } #[test] fn count_collection_equals_for() { - let progress_states = vec![Progress::Complete, Progress::Some, Progress::None]; let collection = get_vec_map(); + let progress_states = [Progress::Complete, Progress::Some, Progress::None]; for progress_state in progress_states { assert_eq!( count_collection_for(&collection, progress_state), - count_collection_iterator(&collection, progress_state) + count_collection_iterator(&collection, progress_state), ); } } - - fn get_map() -> HashMap { - use Progress::*; - - let mut map = HashMap::new(); - map.insert(String::from("variables1"), Complete); - map.insert(String::from("functions1"), Complete); - map.insert(String::from("hashmap1"), Complete); - map.insert(String::from("arc1"), Some); - map.insert(String::from("as_ref_mut"), None); - map.insert(String::from("from_str"), None); - - map - } - - fn get_vec_map() -> Vec> { - use Progress::*; - - let map = get_map(); - - let mut other = HashMap::new(); - other.insert(String::from("variables2"), Complete); - other.insert(String::from("functions2"), Complete); - other.insert(String::from("if1"), Complete); - other.insert(String::from("from_into"), None); - other.insert(String::from("try_from_into"), None); - - vec![map, other] - } } diff --git a/solutions/18_iterators/iterators5.rs b/solutions/18_iterators/iterators5.rs index 4e18198..402c81b 100644 --- a/solutions/18_iterators/iterators5.rs +++ b/solutions/18_iterators/iterators5.rs @@ -1 +1,150 @@ -// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 +// 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 +// number of exercises with a given progress. Recreate this counting +// functionality using iterators. Try to not use imperative loops (for/while). + +use std::collections::HashMap; + +#[derive(Clone, Copy, PartialEq, Eq)] +enum Progress { + None, + Some, + Complete, +} + +fn count_for(map: &HashMap, value: Progress) -> usize { + let mut count = 0; + for val in map.values() { + if *val == value { + count += 1; + } + } + count +} + +fn count_iterator(map: &HashMap, value: Progress) -> usize { + // `map` is a hash map with `String` keys and `Progress` values. + // map = { "variables1": Complete, "from_str": None, … } + map.values().filter(|val| **val == value).count() +} + +fn count_collection_for(collection: &[HashMap], value: Progress) -> usize { + let mut count = 0; + for map in collection { + count += count_for(map, value); + } + count +} + +fn count_collection_iterator(collection: &[HashMap], value: Progress) -> usize { + // `collection` is a slice of hash maps. + // collection = [{ "variables1": Complete, "from_str": None, … }, + // { "variables2": Complete, … }, … ] + collection + .iter() + .map(|map| count_iterator(map, value)) + .sum() +} + +fn main() { + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + fn get_map() -> HashMap { + use Progress::*; + + let mut map = HashMap::new(); + map.insert(String::from("variables1"), Complete); + map.insert(String::from("functions1"), Complete); + map.insert(String::from("hashmap1"), Complete); + map.insert(String::from("arc1"), Some); + map.insert(String::from("as_ref_mut"), None); + map.insert(String::from("from_str"), None); + + map + } + + fn get_vec_map() -> Vec> { + use Progress::*; + + let map = get_map(); + + let mut other = HashMap::new(); + other.insert(String::from("variables2"), Complete); + other.insert(String::from("functions2"), Complete); + other.insert(String::from("if1"), Complete); + other.insert(String::from("from_into"), None); + other.insert(String::from("try_from_into"), None); + + vec![map, other] + } + + #[test] + fn count_complete() { + let map = get_map(); + assert_eq!(count_iterator(&map, Progress::Complete), 3); + } + + #[test] + fn count_some() { + let map = get_map(); + assert_eq!(count_iterator(&map, Progress::Some), 1); + } + + #[test] + fn count_none() { + let map = get_map(); + assert_eq!(count_iterator(&map, Progress::None), 2); + } + + #[test] + fn count_complete_equals_for() { + let map = get_map(); + let progress_states = [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] + fn count_collection_complete() { + let collection = get_vec_map(); + assert_eq!( + count_collection_iterator(&collection, Progress::Complete), + 6, + ); + } + + #[test] + fn count_collection_some() { + let collection = get_vec_map(); + assert_eq!(count_collection_iterator(&collection, Progress::Some), 1); + } + + #[test] + fn count_collection_none() { + let collection = get_vec_map(); + assert_eq!(count_collection_iterator(&collection, Progress::None), 4); + } + + #[test] + fn count_collection_equals_for() { + let collection = get_vec_map(); + let progress_states = [Progress::Complete, Progress::Some, Progress::None]; + + for progress_state in progress_states { + assert_eq!( + count_collection_for(&collection, progress_state), + count_collection_iterator(&collection, progress_state), + ); + } + } +} -- cgit v1.2.3