Skip to content

Possible to mutably borrow elements inside a Vec inside a HashMap multiple times #17500

Closed
@tedsta

Description

@tedsta

Sorry, I'm not sure how to describe this very well in a sentence. I am able to mutably borrow the innermost values of a HashMap<u32, Vec<int>>, delete the values, then reference them after they were deleted. This code compiles, runs, and barfs out strange numbers.

use std::collections::HashMap;

fn main() {
    let mut my_stuff: HashMap<u32, Vec<int>> = HashMap::new();
    my_stuff.insert(0, vec![1, 2, 3, 4]);

    // Do bad things
    let mut stuff = vec!();
    for (_, vec) in my_stuff.mut_iter() {
        for thing in vec.mut_iter() {
            stuff.push(thing);
        }
    }

    my_stuff.clear();

    for thing in stuff.iter() {
        println!("{}", *thing);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions