mirror of https://github.com/01-edu/public.git
Augusto
4 years ago
4 changed files with 181 additions and 0 deletions
@ -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", |
||||||
|
] |
@ -0,0 +1,10 @@ |
|||||||
|
[package] |
||||||
|
name = "roman_numbers_test" |
||||||
|
version = "0.1.0" |
||||||
|
authors = ["Augusto <aug.ornelas@gmail.com>"] |
||||||
|
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"} |
@ -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 |
||||||
|
); |
||||||
|
} |
@ -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$ |
||||||
|
``` |
Loading…
Reference in new issue