#mismatched types error

9 messages · Page 1 of 1 (latest)

keen shoal
#

Hello, I am new to rust and am struggling to get my head around a few concepts. I understand the error of my code but dont really know how to fix it

use std::collections::HashMap;
// Lexical Analysis Errors

//
struct LexicalAnalyzer<'a> {
    function_map: &'a mut HashMap<&'a str, &'a str>,
}
enum Tokens {
    ParenthesisLeft,
    ParenthesisRight,
    Function,
    Exponent,
    Mul,
    Div,
    Add,
    Sub,
    Procedure(i32),
    Number(i32),
}
impl<'a> LexicalAnalyzer<'a> {
    fn clean(&mut self) {
        self.function_map = self
            .function_map
            .iter_mut()
            .map(|(name, function)| (*name, &function.replace(" ", "") as &str))
            .collect::<HashMap<&str, &str>>();
    }

the clean function is the source of the error. I want to mutate self.function_map

limpid slate
#

your lexical analyzer is defined to hold a reference to a HashMap, but you're trying to give it an owned HashMap

keen shoal
#

oh nvm fixed

#

it

#

lol

#

thnx

violet plaza
#

and if you had given it a borrowed HashMap, you would have gotten a useless perpetual borrow

#

when you have &'a mut, 'a needs to be separate from any lifetimes in its contents