Skip to content

IntoIterator for fixed-size arrays shouldn't borrow the original #39257

Closed
@jdm

Description

@jdm
fn main() {
    let _v = {
        [0, 1].into_iter()
    }.collect::<Vec<_>>();
}

This yields:

rustc 1.14.0 (e8a012324 2016-12-16)
error: borrowed value does not live long enough
 --> <anon>:5:9
  |
5 |         [0, 1].into_iter()
  |         ^^^^^^ temporary value created here
6 |     }.collect::<Vec<_>>();
  |                          - temporary value only lives until here
7 | }
  | - temporary value needs to live until here
  |
  = note: consider using a `let` binding to increase its lifetime

This is very confusing conceptually, since into_iter() is supposed to consume the value it iterates over. Presumably this happens because IntoIterator is only implemented for &[T; N] and &mut [T; N], and not [T; N].

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions