mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 09:48:45 +00:00
Add exercise traits6 practicing extension traits
This commit is contained in:
parent
9b50da484f
commit
05d0b14a58
@ -9,6 +9,7 @@
|
|||||||
- New argument `--no-editor` to disable automatic opening of the current file in VS Code or Zellij
|
- 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
|
- 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`
|
- Show the file link of the current exercise when running `rustlings hint` and `rustlings reset`
|
||||||
|
- Add a new `traits6` exercise.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
|||||||
@ -126,6 +126,8 @@ bin = [
|
|||||||
{ name = "traits4_sol", path = "../solutions/15_traits/traits4.rs" },
|
{ name = "traits4_sol", path = "../solutions/15_traits/traits4.rs" },
|
||||||
{ name = "traits5", path = "../exercises/15_traits/traits5.rs" },
|
{ name = "traits5", path = "../exercises/15_traits/traits5.rs" },
|
||||||
{ name = "traits5_sol", path = "../solutions/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", path = "../exercises/quizzes/quiz3.rs" },
|
||||||
{ name = "quiz3_sol", path = "../solutions/quizzes/quiz3.rs" },
|
{ name = "quiz3_sol", path = "../solutions/quizzes/quiz3.rs" },
|
||||||
{ name = "lifetimes1", path = "../exercises/16_lifetimes/lifetimes1.rs" },
|
{ name = "lifetimes1", path = "../exercises/16_lifetimes/lifetimes1.rs" },
|
||||||
|
|||||||
33
exercises/15_traits/traits6.rs
Normal file
33
exercises/15_traits/traits6.rs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -793,6 +793,16 @@ replacing `???` with 'impl [what goes here?] + [what goes here?]'.
|
|||||||
Related section in The Book:
|
Related section in The Book:
|
||||||
https://doc.rust-lang.org/book/ch10-02-traits.html#specifying-multiple-trait-bounds-with-the--syntax"""
|
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
|
# QUIZ 3
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
|||||||
53
solutions/15_traits/traits6.rs
Normal file
53
solutions/15_traits/traits6.rs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
trait Digits {
|
||||||
|
fn digits(&self) -> Vec<usize>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Digits for usize {
|
||||||
|
fn digits(&self) -> Vec<usize> {
|
||||||
|
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<usize> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user