如何比较两个迭代器

时间:2019-05-18 22:43:52

标签: rust iterator

我有一个工作的斐波那契迭代器,我想将其与前10个已知元素的迭代器进行比较。

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
    return footerView
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 50
}

错误消息是

pub struct FibonacciSequence {
    prev: u32,
    curr: u32,
}

impl Iterator for FibonacciSequence {
    type Item = u32;
    fn next(&mut self) -> Option<Self::Item> {
        let next = self.prev + self.curr;
        self.prev = self.curr;
        self.curr = next; // sequence is infinite
        Some(self.curr)
    }
}

pub fn fib_iter() -> FibonacciSequence {
    FibonacciSequence { prev: 0, curr: 1 }
}

#[test]
fn test_fib_iter() {
    assert!(vec![1 as u32, 2, 3, 5, 8, 13, 21, 34, 55, 89]
        .iter()
        .eq(fib_iter().take(10)));
}

比较两个向量可以正常工作:

error[E0277]: can't compare `&u32` with `u32`
  --> src/lib.rs:24:10
   |
24 |         .eq(fib_iter().take(10)));
   |          ^^ no implementation for `&u32 == u32`
   |
   = help: the trait `std::cmp::PartialEq<u32>` is not implemented for `&u32`

0 个答案:

没有答案