This is my first bit of Rust code. I added the lifetime parameters to the inner function only because the compiler told me to. Although I understand the lifetime explanation in the Rust book, I couldn't have written this signature by myself.
fn transform_the_expression() {
fn create_formula<'t>(formula: & mut HashMap<& str, &'t str>, input: &'t str, re: Regex)->std::borrow::Cow<'t, str>{
let replacement = re.find(input).unwrap();
formula.insert("1", replacement.as_str());
let rest = re.replace(input, "1");
return rest;
}
let input = "(a+(b*c))";
use regex::Regex;
let re = Regex::new(r"\([\w\d][/*+-^][\w\d]\)").unwrap();
use std::collections::HashMap;
let mut formula = HashMap::new();
let result = create_formula(&mut formula, input, re);
println!("result = {:?}", result);
}
- Why do I need lifetimes at these 3 places in the signature?
- Why don't I need them at the other places?
- How I would go about writing the correct signature without the compiler telling me what to do?
- How can I identify the parameters needing lifetimes?