From 473ff15a2031ff2ade91cdea5ef92301d3de61c3 Mon Sep 17 00:00:00 2001 From: Augusto Date: Mon, 28 Dec 2020 17:24:11 +0000 Subject: [PATCH] Add subject and test for exercise `roman_numbers_iter` --- rust/tests/roman_numbers_iter_test/Cargo.lock | 12 +++ rust/tests/roman_numbers_iter_test/Cargo.toml | 10 +++ .../tests/roman_numbers_iter_test/src/main.rs | 85 +++++++++++++++++++ subjects/roman_numbers_iter/README.md | 74 ++++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 rust/tests/roman_numbers_iter_test/Cargo.lock create mode 100644 rust/tests/roman_numbers_iter_test/Cargo.toml create mode 100644 rust/tests/roman_numbers_iter_test/src/main.rs create mode 100644 subjects/roman_numbers_iter/README.md diff --git a/rust/tests/roman_numbers_iter_test/Cargo.lock b/rust/tests/roman_numbers_iter_test/Cargo.lock new file mode 100644 index 000000000..a532508e0 --- /dev/null +++ b/rust/tests/roman_numbers_iter_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "roman_numbers" +version = "0.1.0" + +[[package]] +name = "roman_numbers_test" +version = "0.1.0" +dependencies = [ + "roman_numbers", +] diff --git a/rust/tests/roman_numbers_iter_test/Cargo.toml b/rust/tests/roman_numbers_iter_test/Cargo.toml new file mode 100644 index 000000000..195c8f671 --- /dev/null +++ b/rust/tests/roman_numbers_iter_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "roman_numbers_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +roman_numbers = { path = "../../../../rust-piscine-solutions/roman_numbers"} diff --git a/rust/tests/roman_numbers_iter_test/src/main.rs b/rust/tests/roman_numbers_iter_test/src/main.rs new file mode 100644 index 000000000..8d9ed8cfb --- /dev/null +++ b/rust/tests/roman_numbers_iter_test/src/main.rs @@ -0,0 +1,85 @@ +// # Instructions +// Implement the IntoIterator trait for the `RomanNumber` type to +// enable using a for loop notation. +// This implementation must allow taking ownership, +// borrowing and borrowing mutably + +// I.e. this three constructions must be possible +// ```rust +// let number = RomanNumber::from(23); + +// 1. Taking ownership (this consumes the RomanNumber) +// for digit in number { +// ... +// } + +// 2. Borrowing immutably (this preserves the RomanNumber) +// for digit in &number { + +// } + +// 3. Borrowing mutably (this allow you to modify the RomanNumber +// without having to return the ownership) +// for digit in &mut number { + +// } + +// Start with your research See https://doc.rust-lang.org/std/iter/trait.IntoIterator.html +// https://doc.rust-lang.org/std/iter/index.html + +use roman_numbers::{RomanDigit, RomanNumber}; + +fn main() { + let number = RomanNumber::from(15); + + for digit in &number { + println!("{:?}", digit); + } + println!("{:?}", number); +} + +#[allow(dead_code)] +fn into_u32(n: RomanDigit) -> u32 { + use RomanDigit::*; + match n { + Nulla => 0, + I => 1, + V => 5, + X => 10, + L => 50, + C => 100, + D => 500, + M => 1000, + } +} + +#[test] +fn test_iter() { + let number = RomanNumber::from(15); + + for digit in &number { + println!("{:?}", digit); + } + println!("{:?}", number); +} + +#[test] +fn test_into_iter() { + let number = RomanNumber::from(37); + let value: u32 = number.into_iter().map(|digit| into_u32(digit)).sum(); + println!("value: {}", value); +} + +#[test] +fn test_iter_mut() { + let mut number = RomanNumber::from(22); + + for digit in &mut number { + let value = into_u32(*digit); + *digit = dbg!(RomanNumber::from(value - 1)).0[0]; + } + println!( + "Roman Number after increasing the each digit by 1 = {:?}", + number + ); +} diff --git a/subjects/roman_numbers_iter/README.md b/subjects/roman_numbers_iter/README.md new file mode 100644 index 000000000..4f03ab396 --- /dev/null +++ b/subjects/roman_numbers_iter/README.md @@ -0,0 +1,74 @@ +## roman_numbers_iter + +### Instructions + +Implement the IntoIterator trait for the `RomanNumber` type to enable using a for loop notation. This implementation must allow taking ownership, borrowing and borrowing mutably + +1. Taking ownership (this consumes the RomanNumber) +```rust +for digit in number { + ... +} +``` + +2. Borrowing immutably (this preserves the RomanNumber) +```rust + for digit in &number { + + } +``` + +3. Borrowing mutably (this allow you to modify the RomanNumber without having to return the ownership) +```rust + for digit in &mut number { + + } +``` + +### Notions + +- https://doc.rust-lang.org/std/iter/trait.IntoIterator.html +- https://doc.rust-lang.org/std/iter/index.html + +### Expected Functions + +```rust +use roman_numbers::{RomanDigit, RomanNumber}; + +impl IntoIterator for &RomanNumber { +} + +impl IntoIterator for &mut RomanNumber { +} + +impl IntoIterator for RomanNumber { +} +``` + +### Usage + +Here is a program to test your function. + +```rust +use roman_numbers::RomanNumber; + +fn main() { + let number = RomanNumber::from(15); + + for digit in &number { + println!("{:?}", digit); + } + println!("{:?}", number); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +RomanNumber([X, X, X, I, I]) +RomanNumber([I, X]) +RomanNumber([X, L, V]) +RomanNumber([Nulla]) +student@ubuntu:~/[[ROOT]]/test$ +```