I've encountered a simple task: I want to pass a sum of strings to a constructor of an object. In my case, I wanted to create a Path
from a sum of &str
, like this:
const BASE_PATH: &str = "/some/path/";
fn main() {
let write_br_path = Path::new(BASE_PATH + "brightness");
}
However, I soon found out that I can't sum &str and &str, so, as my compiler suggested, I did something like this:
let write_br_path = Path::new(&(BASE_PATH.to_owned() + "brightness"));
However, this also didn't work. This code failed to compile, informing me that "creates a temporary which is freed while still in use". I've come up with a simple solution:
let m_b = BASE_PATH.to_owned() + "max_brightness";
let max_br_path = Path::new(&(m_b));
This leaves me with a question: can this code be written in a more compact way, without additional variable being created?