diff --git a/CHANGELOG.md b/CHANGELOG.md index e2acf6c6..2132b648 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - New argument `--no-editor` to disable automatic opening of the current file in VS Code or Zellij - New argument `--edit-cmd` to communicate with an editor running in a different process to open the current exercise - Show the file link of the current exercise when running `rustlings hint` and `rustlings reset` +- Add a new `traits6` exercise. ### Fixed diff --git a/dev/Cargo.toml b/dev/Cargo.toml index 66bc1dfe..4bcea6ea 100644 --- a/dev/Cargo.toml +++ b/dev/Cargo.toml @@ -126,6 +126,8 @@ bin = [ { name = "traits4_sol", path = "../solutions/15_traits/traits4.rs" }, { name = "traits5", path = "../exercises/15_traits/traits5.rs" }, { name = "traits5_sol", path = "../solutions/15_traits/traits5.rs" }, + { name = "traits6", path = "../exercises/15_traits/traits6.rs" }, + { name = "traits6_sol", path = "../solutions/15_traits/traits6.rs" }, { name = "quiz3", path = "../exercises/quizzes/quiz3.rs" }, { name = "quiz3_sol", path = "../solutions/quizzes/quiz3.rs" }, { name = "lifetimes1", path = "../exercises/16_lifetimes/lifetimes1.rs" }, diff --git a/exercises/15_traits/traits6.rs b/exercises/15_traits/traits6.rs new file mode 100644 index 00000000..0c873567 --- /dev/null +++ b/exercises/15_traits/traits6.rs @@ -0,0 +1,33 @@ +// TODO: Design a trait that enables you to calculate the digit sum over both an +// integer as well as a string. + +// TODO: Change this function signature so it works with both integers and +// strings, based on your trait. +fn calculate_digit_sum(number: ???) -> usize { + // TODO: Calculate the digit sum of `number`. +} + +fn main() { + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn integer() { + assert_eq!(calculate_digit_sum(0), 1); + assert_eq!(calculate_digit_sum(1), 1); + assert_eq!(calculate_digit_sum(1235), 11); + assert_eq!(calculate_digit_sum(789), 24); + } + + #[test] + fn string() { + assert_eq!(calculate_digit_sum(""), 1); + assert_eq!(calculate_digit_sum("1"), 1); + assert_eq!(calculate_digit_sum("1235"), 11); + assert_eq!(calculate_digit_sum("789"), 24); + } +} diff --git a/rustlings-macros/info.toml b/rustlings-macros/info.toml index 637283d2..6edc3ac8 100644 --- a/rustlings-macros/info.toml +++ b/rustlings-macros/info.toml @@ -793,6 +793,16 @@ replacing `???` with 'impl [what goes here?] + [what goes here?]'. Related section in The Book: https://doc.rust-lang.org/book/ch10-02-traits.html#specifying-multiple-trait-bounds-with-the--syntax""" +[[exercises]] +name = "traits6" +dir = "15_traits" +hint = """ +Remember that you can implement a trait you defined yourself on foreign types, +i.e. ones from the standard library or third-party libraries. A trait that only +serves the purpose of adding additional functionality to foreign types is often +referred to as an "extension trait". Define such a trait and implement it for +`usize` and `&str`.""" + # QUIZ 3 [[exercises]] diff --git a/solutions/15_traits/traits6.rs b/solutions/15_traits/traits6.rs new file mode 100644 index 00000000..de620cd1 --- /dev/null +++ b/solutions/15_traits/traits6.rs @@ -0,0 +1,53 @@ +trait Digits { + fn digits(&self) -> Vec; +} + +impl Digits for usize { + fn digits(&self) -> Vec { + let mut res = Vec::new(); + let mut temp = *self; + while temp != 0 { + res.push(temp % 10); + temp /= 10; + } + res.reverse(); + res + } +} + +impl Digits for &str { + fn digits(&self) -> Vec { + self.chars() + .map(|c| c.to_digit(10).unwrap() as usize) + .collect() + } +} + +fn calculate_digit_sum(number: impl Digits) -> usize { + number.digits().into_iter().sum() +} + +fn main() { + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn integer() { + assert_eq!(calculate_digit_sum(0), 1); + assert_eq!(calculate_digit_sum(1), 1); + assert_eq!(calculate_digit_sum(1235), 11); + assert_eq!(calculate_digit_sum(789), 24); + } + + #[test] + fn string() { + assert_eq!(calculate_digit_sum(""), 1); + assert_eq!(calculate_digit_sum("1"), 1); + assert_eq!(calculate_digit_sum("1235"), 11); + assert_eq!(calculate_digit_sum("789"), 24); + } +}