Skip to content

Inclusion #80773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
t10-13rocket opened this issue Jan 7, 2021 · 5 comments
Closed

Inclusion #80773

t10-13rocket opened this issue Jan 7, 2021 · 5 comments

Comments

@t10-13rocket
Copy link

#![allow(unused)]
// First, the struct:

fn main() {
/// An iterator which counts from one to five
struct Counter {
    count: usize,
}

// we want our count to start at one, so let's add a new() method to help.
// This isn't strictly necessary, but is convenient. Note that we start
// `count` at zero, we'll see why in `next()`'s implementation below.
impl Counter {
    fn new() -> Counter {
        Counter { count: 0 }
    }
}

// Then, we implement `Iterator` for our `Counter`:

impl Iterator for Counter {
    // we will be counting with usize
    type Item = usize;

    // next() is the only required method
    fn next(&mut self) -> Option<Self::Item> {
        // Increment our count. This is why we started at zero.
        self.count += 1;

        // Check to see if we've finished counting or not.
        if self.count < 6 {
            Some(self.count)
        } else {
            None
        }
    }
}

// And now we can use it!

let mut counter = Counter::new();

assert_eq!(counter.next(), Some(1));
assert_eq!(counter.next(), Some(2));
assert_eq!(counter.next(), Some(3));
assert_eq!(counter.next(), Some(4));
assert_eq!(counter.next(), Some(5));
assert_eq!(counter.next(), None);
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished release [optimized] target(s) in 1.17s
     Running `target/release/playground`

@t10-13rocket
Copy link
Author

Should be optional for inclusion of alphabets of all languages.

@SkiFire13
Copy link
Contributor

Note that your code has a bug in release mode because self.count could overflow

@jonas-schievink
Copy link
Contributor

@t10-13rocket I'm not sure what you are asking for, can you clarify?

@tesuji
Copy link
Contributor

tesuji commented Jan 7, 2021

@t10-13rocket The Rust repo has issue templates that ones can fill in to help
readers understand the problem in the issue. Could you use that?

@nagisa
Copy link
Member

nagisa commented Jan 8, 2021

Note that the code snippet is copied from the documentation of std::iter.

This issue might be spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants