0

I am yet another beginner in Rust who have trouble converting between my types. It boils down to that I have a HashMap<String, Type> and &[Type] as input to another function.

The doesn't need to be fast or efficient, so an elegant solution is preferred to an efficient one.

My HashMap is in the variable called "parsed_result" and I've tried to convert it like this:

let mut secs: Vec<_> = parsed_result.values().map(|ms|ms.clone()).collect();
let mem_map = mem_planner::plan1(&secs);

However, I only get the following compile error.

error[E0308]: mismatched types
  --> src\main.rs:17:38
   |
17 |     let mem_map = mem_planner::plan1(&secs);
   |                                      ^^^^^ expected slice, found struct `std::vec::Vec`
   |
   = note: expected type `&[map_parser::MemorySection]`
              found type `&std::vec::Vec<&map_parser::MemorySection>`

I do understand the error, but I have no idea to do the required transformation.

If I just call the function with ´parsed_result´ I get this error (should show the type of `parsed_result´)

error[E0308]: mismatched types
  --> src\main.rs:24:38
   |
24 |     let mem_map = mem_planner::plan1(parsed_result);
   |                                      ^^^^^^^^^^^^^ expected reference, found struct `std::collections::HashMap`
   |
   = note: expected type `&[map_parser::MemorySection]`
              found type `std::collections::HashMap<std::string::String, map_parser::MemorySection>`

Minimal working example:

mem_parser.rs

use std::collections::HashMap;

pub struct MemoryObject {
    name: String,
    size: usize,
}

pub struct MemorySection {
    pub name: String,
    pub size: usize,
    pub objs: Vec<MemoryObject>,
}

pub type MemMap = HashMap<String, MemorySection>;

pub fn parse() -> MemMap {
    let mut memory_sections = HashMap::new();

    let section_name = "key".to_string();
    let section_val = MemorySection {name: "readable name".to_string(), size: 10, objs: Vec::new() };

    memory_sections.insert(section_name, section_val);

    memory_sections

}

main.rs

mod mem_parser;

fn main() {

    let parsed_result = mem_parser::parse();

    let mut secs: Vec<_> = parsed_result.values().map(|ms| ms.clone()).collect();
    let mem_map = plan1(&secs);
}

fn plan1(objects: &[mem_parser::MemorySection]) -> () {
    println!("Test");
}
Peter Ludvigsen
  • 320
  • 3
  • 12

0 Answers0