DEV Community

Discussion on: Rust lifetimes, a high wall for Rust newbies

Collapse
 
serak profile image
Serak Shiferaw

2 question here, if the compiler knows i should add lifetime at specified points why not just do it itself. instead of telling me to add 'a everywhere the other is why didnt rust used the old c style &str

e.g

[allow(unused_variables)]

fn main() {
let string1 = String::from("abcd");
let string2 = "xyz";

let result = longest(&string1.as_str(), &string2); //notice & to pass by reference and enforce the compiler to pass lifetime to the called function 

}

fn longest(x: &str, y: &str) -> &str {
if x.len() > y.len() {
x
} else {
y
}
}