Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to iterate over &mut Iterator using a for loop, results in lifetime error #21655

Closed
japaric opened this issue Jan 26, 2015 · 3 comments
Closed
Labels
A-type-system Area: Type system

Comments

@japaric
Copy link
Member

japaric commented Jan 26, 2015

STR

fn test(it: &mut Iterator<Item=()>) {
    for _ in it {}
}

fn main() {}

Output

for.rs:2:14: 2:16 error: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
for.rs:2     for _ in it {}
                      ^~
for.rs:2:14: 2:16 error: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
for.rs:2     for _ in it {}
                      ^~
for.rs:2:14: 2:16 error: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
for.rs:2     for _ in it {}
                      ^~
for.rs:1:1: 3:2 help: consider using an explicit lifetime parameter as shown: fn test<'a>(it: &'a mut Iterator<Item = ()>)
for.rs:1 fn test(it: &mut Iterator<Item=()>) {
for.rs:2     for _ in it {}
for.rs:3 }
error: aborting due to 3 previous errors

Version

rustc 1.0.0-nightly (458a6a2f6 2015-01-25 21:20:37 +0000)

cc @nikomatsakis

@japaric
Copy link
Member Author

japaric commented Jan 26, 2015

This compiles with the new for loops (#20790).

@bluss
Copy link
Member

bluss commented Jan 26, 2015

Isn't it the same with non-trait &mut T iterators? I'd use for elt in *it.

@japaric
Copy link
Member Author

japaric commented Jan 26, 2015

@bluss for _ in it when it: &mut Iterator results in an ICE (see #21379). Also this should just work, because &mut Iterator implements the Iterator trait.

@kmcallister kmcallister added the A-type-system Area: Type system label Jan 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

3 participants