Browse Source

roman numbers iter update

pull/1084/head
davidrobert99 3 years ago
parent
commit
b5b6ecc51e
  1. 51
      subjects/roman_numbers_iter/README.md

51
subjects/roman_numbers_iter/README.md

@ -2,50 +2,19 @@
### 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.
Implement the `Iterator` trait for the `RomanNumber` type. You should use the code from the previous exercise roman_numbers.
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
- [Trait Iterator](https://doc.rust-lang.org/std/iter/trait.Iterator.html)
### Expected Functions
```rust
use roman_numbers::{RomanDigit, RomanNumber};
//...
impl IntoIterator for &RomanNumber {
}
impl IntoIterator for &mut RomanNumber {
}
impl IntoIterator for RomanNumber {
}
impl Iterator for RomanNumber {}
```
### Usage
@ -56,12 +25,10 @@ Here is a program to test your function.
use roman_numbers::RomanNumber;
fn main() {
let number = RomanNumber::from(15);
let mut number = RomanNumber::from(15);
for digit in &number {
println!("{:?}", digit);
}
println!("{:?}", number);
println!("{:?}", number.next());
}
```
@ -69,9 +36,7 @@ And its output
```console
$ cargo run
RomanNumber([X, X, X, I, I])
RomanNumber([I, X])
RomanNumber([X, L, V])
RomanNumber([Nulla])
RomanNumber([X, V])
Some(RomanNumber([X, V, I]))
$
```

Loading…
Cancel
Save