std-method

Iterator::step_by — sample regular positions

Yield the first item, then every step-th item. The step must be positive; a zero step panics. The exact timing of skipped-item advancement is not specified. Expected stdout (not executed): [1, 4, 7, 10] Common mistake: Passing zero or assuming skipped-item side effects occur at a fixed time.

Formal Syntax & Structure

rust syntaxrust
fn step_by(self, step: usize) -> StepBy<Self> where Self: Sized

Working Example

Example Snippetrust
fn main() {    let samples: Vec<_> = (1..=10).step_by(3).collect();    println!("{samples:?}");}
Rust Reference