Just ran into this one while writing tests. Maybe more an issue with macros, asserts, or just the way we automatically make a convenience reference to a literal, but it appears as a lifetime issue:
fn main() {
let mut a = vec![1i,2,3];
let mut b = a.mut_iter();
assert_eq!(b.next(), Some(&mut 1i));
}
<anon>:4:36: 4:38 error: borrowed value does not live long enough
<anon>:4 assert_eq!(b.next(), Some(&mut 1i));
^~
<std macros>:1:1: 14:2 note: in expansion of assert_eq!
<anon>:4:5: 4:41 note: expansion site
<anon>:1:11: 5:2 note: reference must be valid for the block at 1:10...
<anon>:1 fn main() {
<anon>:2 let mut a = vec![1i,2,3];
<anon>:3 let mut b = a.mut_iter();
<anon>:4 assert_eq!(b.next(), Some(&mut 1i));
<anon>:5 }
<std macros>:2:40: 13:6 note: ...but borrowed value is only valid for the statement at 2:39; consider using a `let` binding to increase its lifetime
<std macros>:2 ($given:expr , $expected:expr) => ({
<std macros>:3 match (&($given), &($expected)) {
<std macros>:4 (given_val, expected_val) => {
<std macros>:5 // check both directions of equality....
<std macros>:6 if !((*given_val == *expected_val) &&
<std macros>:7 (*expected_val == *given_val)) {
...
<std macros>:1:1: 14:2 note: in expansion of assert_eq!
<anon>:4:5: 4:41 note: expansion site
error: aborting due to previous error
fixable by moving the iterator’s creation into the assert:
fn main() {
let mut a = vec![1i,2,3];
assert_eq!(a.mut_iter().next(), Some(&mut 1i));
}
but that’s not really useful if you want to test a whole bunch of values.
This also works, and actually lets you reuse the iterator, but is of course annoying:
fn main () {
let mut a = vec![1i,2,3];
let mut b = a.mut_iter();
assert_eq!(&*b.next().unwrap(), &1);
}