Skip to content

Instantly share code, notes, and snippets.

@Luctins
Last active Jul 21, 2022
Embed
What would you like to do?
Little ASCII spinner I wrote in rust to learn how to use iterators
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])
}
}
@Luctins
Copy link
Author

Luctins commented Jul 21, 2022

Update: later found out you can do all of this in a single line (thanks to @Winggel) :

"|/-\\".chars().cycle()

but this will be kept up as a example, where all is spelled out explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment