Debian Codegraph @

third_party/rust/crossbeam-channel/examples/fibonacci.rs

raw
1 //! An asynchronous fibonacci sequence generator.
2
3 use std::thread;
4
5 use crossbeam_channel::{bounded, Sender};
6
7 // Sends the Fibonacci sequence into the channel until it becomes disconnected.
8 fn fibonacci(sender: Sender<u64>) {
9 let (mut x, mut y) = (0, 1);
10 while sender.send(x).is_ok() {
11 let tmp = x;
12 x = y;
13 y += tmp;
14 }
15 }
16
17 fn main() {
18 let (s, r) = bounded(0);
19 thread::spawn(|| fibonacci(s));
20
21 // Print the first 20 Fibonacci numbers.
22 for num in r.iter().take(20) {
23 println!("{}", num);
24 }
25 }