Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter ~[T]

Tags:

rust

This doesn't seems to work for me (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();
like image 289
rofrol Avatar asked Dec 17 '25 15:12

rofrol


1 Answers

The problem is in your collect() call. The iterator is over &uint, and so collect() would expect to produce something like ~[&uint].

You should change it to use move_iter() instead of iter(), or else put in a .map(|&n| n).

like image 126
Chris Morgan Avatar answered Dec 19 '25 05:12

Chris Morgan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!