DEV Community

Discussion on: Challenge: Write a program that never stops

Collapse
 
gefjon profile image
Phoebe Goldman

Rust:

fn main() {
    loop {
    }
}

Or, for a slightly more involved example:

use std::num::Wrapping;
use std::iter::Iterator;

struct Fibs {
    n: Wrapping<usize>,
    n_minus_one: Wrapping<usize>,
}

impl Fibs {
    fn new() -> Self {
        Fibs {
            n: Wrapping(1),
            n_minus_one: Wrapping(0),
        }
    }
}

impl Iterator for Fibs {
    type Item = Wrapping<usize>;
    fn next(&mut self) -> Option<Self::Item> {
        let n_plus_one = self.n + self.n_minus_one;
        self.n_minus_one = self.n;
        self.n = n_plus_one;
        Some(n_plus_one)
    }
}

fn main() {
    let mut fibs = Fibs::new();
    while let Some(n) = fibs.next() {
        println!("{}", n);
    }
}