Parser Implementation (2)
- Liu, An-Chi 劉安齊
Working on the parser. When I write the following function
Parser::new, I encounter lifetime issue. No matter how I add lifetime syntax
b in the function. I cannot pass the compiler.
// The old version
The error is:
error[E0597]: `s` does not live long enough
That’s very wierd, because I expect the lifetime is correct. Anyway, I give up fighting with lifetime.
Therefore, I change the type of
String rather than
&str. Then all errors exist, and I don’t need to add lifetime syntax anymore.
It works very well.
This story tells us, when you implement a
struct in Rust, it is better to let the type
String in the fields. Though it will be stored at heap, but it will be easier with lifetime handling – once using
&str as type, you must add
'a lifetime for the
struct, and all other
struct that use this
struct. Also, all objects related to this
struct in lifetime
'a, will all live as long as
'a, and it seems not a good idea. So,
String next time.