Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 23, 2025 11:52
Show Gist options
  • Select an option

  • Save rust-play/f58a809bb42b1eeeac1ae46ead348bc5 to your computer and use it in GitHub Desktop.

Select an option

Save rust-play/f58a809bb42b1eeeac1ae46ead348bc5 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
//
// A basic struct that holds an array of 16-bit unsigned integers.
struct Numbers([u16; 10]);
// Implement the IntoIterator trait for our Numbers struct.
impl<'a> IntoIterator for &'a Numbers {
// We want to iterate over references to u16 values.
type Item = &'a u16;
// The type of the iterator we're returning.
type IntoIter = std::slice::Iter<'a, u16>;
// The method that creates and returns the iterator.
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
fn main() {
let numbers = Numbers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
let (evens, odds): (std::vec::Vec<u16>, std::vec::Vec<u16>) =
numbers.into_iter().partition(|&n| n % 2 == 0);
for n in &evens {
println!("even:{}", n);
}
for n in &odds {
println!("odd:{}", n);
}
for n in &numbers {
println!("numbers:{}", n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment