如何过滤〜[T]

时间:2014-04-28 08:46:18

标签: rust

这对我来说似乎不起作用(rust-0.10)

let x : ~[uint] = ~[1,2,3];
let sieved: ~[uint] = x.iter().filter(|&n| 3 % n == 0).collect();
...
prob0003.rs:49:77: 49:78 error: mismatched types: expected `<generic integer #5>` but found `&uint` (expected &-ptr but found integral variable)
prob0003.rs:49  let x : ~[uint] = ~[1,2,3]; let sieved: ~[uint] = x.iter().filter(|&n| 3 % n == 0).collect();

1 个答案:

答案 0 :(得分:5)

问题出在您的collect()电话中。迭代器超过&uint,因此collect()会产生类似~[&uint]的内容。

您应该将其更改为使用move_iter()而不是iter(),否则请加入.map(|&n| n)