The solution is to use the where clause like this:
extern crate num; use num::One; use std::ops::Add; pub struct Fibonacci<T> { nth: T, n_plus_one_th: T, } impl<T> Fibonacci<T> where T: One { pub fn new() -> Fibonacci<T> { Fibonacci { nth: T::one(), n_plus_one_th: T::one(), } } } impl<T> Iterator for Fibonacci<T> where for<'a> &'a T: Add<&'a T, Output = T> { type Item = T; fn next(&mut self) -> Option<T> { use std::mem::swap; let mut temp = &self.nth + &self.n_plus_one_th; swap(&mut self.nth, &mut temp); swap(&mut self.n_plus_one_th, &mut self.nth); Some(temp) } }
In particular, the sentence for<'a> &'a T: Add<&'a T, Output=T> reads as "for any lifetime 'a , &'a T should implement Add with RHS &'a T and Output=T That is, you can add two &T to get a new T
However, the only remaining problem is shuffling the values ββaround, which can be done using swap .
I also took the liberty of simplifying the restrictions elsewhere (you only need One , not Integer ).
source share