Skip to content

Instantly share code, notes, and snippets.

@Luctins
Last active July 21, 2022 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luctins/99ea0262471139e503fbe44d7ddc11bf to your computer and use it in GitHub Desktop.
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
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