Skip to content

Should there be a compiler error here? #226

Closed
@LucasSte

Description

@LucasSte

I created the following example code:

use indexmap::IndexMap;

struct SaveSmth {
    pub a: usize,
}
fn main() {
    let mut imp : IndexMap<usize, SaveSmth> = IndexMap::new();
    imp.insert(8, SaveSmth{a : 3});

    let mut vec : Vec<(usize, usize)> = Vec::new();

    vec.push((8, 1));

    for item in vec {
        let a = &imp[item.0];
        println!("{}", a.a);
    }
}

Rust panics in let a = &imp[item.0] and the correct way to fetch the value should be let a = &imp[&item.0];. Shouldn't there be a compiler error because I'm passing the wrong type to look up the table?

The panic message says "index out of bounds":

thread 'main' panicked at 'IndexMap: index out of bounds', src/main.rs:16:18

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions