Skip to content

Instantly share code, notes, and snippets.

@ivan
Created January 10, 2016 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivan/9fa904da8857509c2811 to your computer and use it in GitHub Desktop.
Save ivan/9fa904da8857509c2811 to your computer and use it in GitHub Desktop.
extern crate num;
use num::{BigUint, Zero, One};
use std::mem::replace;
struct Fib {
f1: BigUint,
f2: BigUint
}
impl Iterator for Fib {
type Item = BigUint;
fn next(&mut self) -> Option<BigUint> {
let f0 = replace(&mut self.f1, self.f2.clone());
self.f2 = f0 + &self.f1;
return Some(self.f2.clone());
}
}
fn main() {
let f = Fib {
f1: BigUint::zero(),
f2: BigUint::one()
};
for n in f {
println!("{}", n);
}
}
@ivan
Copy link
Author

ivan commented Jan 10, 2016

Without the first self.f2.clone():

src/bin/fib.rs:14:34: 14:38 error: cannot move out of borrowed content [E0507]
src/bin/fib.rs:14       let f0 = replace(&mut self.f1, self.f2);
                                                       ^~~~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment