Learning Rust is hard
I recently start learning Rust. The language is famous but at the same time is infamous. Everybody says it has ste...
For further actions, you may consider blocking this person and/or reporting abuse
The two lifetimes are necessary because you want the struct field to outlive the containing struct. This cannot be achieved with single lifetime for both the struct and its field.
In this function:
It returns a reference &str that is a field (of a field) of Parser, while the Parser only exists within the function body. In order to make it work, the field of Parser has to borrow a value that has the same lifetime as the return value. That is, as long as the returned value &str has the same lifetime as the input argument context, it does not matter how long the Parser struct lives. In other words, the lifetimes of Parser and context are independent - that is why they have two lifetimes in the struct definition.
To understand it in another way, you can consider the struct constructor of Parser as a new() function, whose input argument has different lifetime than it self's, which is common in function definitions.
In this way, it is like that you have an input argument passed to Parser "function", and returns the same argument data. The argument's lifetime has nothing to do with the Parser "function".
See the working version in Playground.
Love this article and this additional information is great. Thank you for adding it.
I can deal with most of Rust's idiosyncrasies, but with lifetimes I feel like the compiler leads me by the nose. I do as I'm told without knowing and I hate doing that.
I'm confused by this article, because the
parse_context()
function is improper - the version presented consumes thecontext
object, but it borrows it inside. This doesn't make sense :-)The proper version of
parse_context()
borrows thecontext
, and doesn't require two lifetimes:Now, without knowing the... context (no pun intended!), I can't be sure if this was intended, or if it's been an accident.
It's possible that this was a contrived example for the sake of understanding (which is fair, as a matter of fact, I do find this interesting), however, based on the mutability of the fields and the operations (all immutable), it seems to me that this was an accident.
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";
}
fn longest(x: &str, y: &str) -> &str {
if x.len() > y.len() {
x
} else {
y
}
}
How about loops? How can we use lifetimes with loops?
Bravo on the helpful article.
Also, unicorn emoji for this line: "If they go out boundries, compiler kills them, just like a prison."