From b5b6ecc51ea2cde653d10769d8826a9a8cc7f81f Mon Sep 17 00:00:00 2001 From: davidrobert99 Date: Mon, 16 May 2022 16:26:29 +0100 Subject: [PATCH] roman numbers iter update --- subjects/roman_numbers_iter/README.md | 51 +++++---------------------- 1 file changed, 8 insertions(+), 43 deletions(-) diff --git a/subjects/roman_numbers_iter/README.md b/subjects/roman_numbers_iter/README.md index 4277708a1..8aa4e4702 100644 --- a/subjects/roman_numbers_iter/README.md +++ b/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])) $ ```