Skip to content

For loop iteration allows mutating container. #16898

@krdln

Description

@krdln

Following code passes borrow checking leading to serious memory unsafety.

fn main() {
    let mut v = Vec::new();
    for _ in v.iter() {
        v.push('j');
    }
}

Code compiles with &c instead of _ too. Loop fails to compile only in for c in v.iter() case where there's an explicit variable with reference or when the loop is desugared manually.

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