Tin

Examples · learn/11-values · 174 of 339

iterator

examples/learn/11-values/iterator.tintin run examples/learn/11-values/iterator.tin
type Countdown = { n: int }

// next changes the countdown in place (inout) and gives the next number, or None when done
fn Countdown.next(c: inout Countdown): Option<int> {
  if c.n == 0 { return None }
  c.n -= 1
  Some(c.n + 1)
}

fn main() {
  let launch = Countdown { n: 3 }
  for t in launch {
    print("{t}...")
  }
  print("liftoff, and launch.n is still {launch.n}")
}