Last active
July 21, 2022 12:35
-
-
Save Luctins/99ea0262471139e503fbe44d7ddc11bf to your computer and use it in GitHub Desktop.
Little ASCII spinner I wrote in rust to learn how to use iterators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct SpinnyThing { | |
index: usize, | |
} | |
impl SpinnyThing { | |
const V: [&'static str; 4] = ["/","-", "\\", "|"]; | |
fn new() -> SpinnyThing { | |
SpinnyThing { index: 0 } | |
} | |
} | |
impl Iterator for SpinnyThing { | |
type Item = &'static str; | |
fn next (&mut self) -> Option<&'static str> | |
{ | |
self.index = (self.index + 1 ) % SpinnyThing::V.len(); | |
Some(SpinnyThing::V[self.index]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update: later found out you can do all of this in a single line (thanks to @Winggel) :
but this will be kept up as a example, where all is spelled out explicitly.