Skip to content

Compiler suggestion for attempt to move out of indexed content #38887

Closed
@leonardo-m

Description

@leonardo-m

This code:

use std::collections::HashMap;
fn encode2(vals: &[u32], encoding: &HashMap<u32, String>) -> String {
    vals.iter().map(|k| encoding[k]).collect()
}
fn main() {}

It doesn't compile:

error[E0507]: cannot move out of indexed content
 --> ...\test.rs:3:25
  |
3 |     vals.iter().map(|k| encoding[k]).collect()
  |                         ^^^^^^^^^^^ cannot move out of indexed content

    error: aborting due to previous error

Perhaps the compiler could suggest the programmer to add a &* :

vals.iter().map(|k| &*encoding[k]).collect()

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