I am writing a method to loop through a (from, to)
of a map and perform multiple rounds of tmp = tmp.replace(from, to)
. I am still trying to grasp the ownership concepts of Rust
#[macro_use]
extern crate lazy_static;
use std::collections::HashMap;
lazy_static! {
static ref REPLACEMENTS: HashMap<&'static str, &'static str> = {
let mut m = HashMap::new();
m.insert("abc", "def");
m.insert("com", "org");
m
};
}
fn replace_path_name(path: &str) -> &str {
let mut tmp = path;
for (from, to) in REPLACEMENTS.iter() {
let a = *from;
let b = *to;
tmp = tmp.replace(a, b);
}
tmp
}
fn main() {}
This code gets me...
error[E0308]: mismatched types
--> src/main.rs:22:15
|
22 | tmp = tmp.replace(a, b);
| ^^^^^^^^^^^^^^^^^
| |
| expected &str, found struct `std::string::String`
| help: consider borrowing here: `&tmp.replace(a, b)`
|
= note: expected type `&str`
found type `std::string::String`
The extra a
and b
are my attempts to get by why Rust made from
and to
become &&str
.