Bjarne Stroustrup’s Plan for Bringing Safety to C++ (The New Stack)
Bjarne Stroustrup’s Plan for Bringing Safety to C++ (The New Stack)
Posted Nov 1, 2023 9:10 UTC (Wed) by tialaramex (subscriber, #21167)In reply to: Bjarne Stroustrup’s Plan for Bringing Safety to C++ (The New Stack) by mb
Parent article: Bjarne Stroustrup’s Plan for Bringing Safety to C++ (The New Stack)
One thing I'd suggest may help with that, try being unnecessarily explicit until you're more comfortable.
When you write &str in Rust, the inference engine (or if we're writing a function signature, specifically Lifetime Elision) tries to figure out what lifetime that reference has and silently assumes that's what we meant, but while this is idiomatic if you're learning lifetimes it may be helpful to write more of them explicitly instead.
Also, much like Physicists writing Fortran think v and k are perfectly good names for important long-lived variables, the Rust lifetimes you see in real code often have absurdly terse names, but that's not mandatory, it's fine to give them longer names.
fn greet<'brief>() {
let s: &'brief str = "Hello there";
println!("{s}");
}
There's no need to name this lifetime 'brief, but by doing so maybe we get more used to seeing lifetimes around and we learn where they're needed even though the language is good at inferring them so you can get away without writing them down.
As a halfway point, you can write the underscore to say you know a lifetime goes here but that lifetime can be inferred.
fn greet() {
let s: &'_ str = "Hello there";
println!("{s}");
}
I found lifetimes very easy to get used to, but I think they're the "weirdest" syntactical change from other semicolon languages. There are a bunch of important semantic changes (notice the semicolon in Rust isn't even doing the same thing as in a language like C or Java) but the syntax is what gets in your eyes immediately.
Posted Nov 1, 2023 9:51 UTC (Wed)
by mb (subscriber, #50428)
[Link] (1 responses)
Well, then just do it this way, if you think it's better?
>notice the semicolon in Rust isn't even doing the same thing as in a language like C or Java
Why is it different? It terminates a statement. How is that different from C?
>I found lifetimes very easy to get used to, but I think they're the "weirdest" syntactical change
That's because almost no language has lifetime annotations.
Posted Nov 1, 2023 22:19 UTC (Wed)
by tialaramex (subscriber, #21167)
[Link]
C is a statement oriented language, whereas Rust is expression oriented. So e.g. this Rust is fine:
let x = if even { 6 } else { 7 }; // The type of the if expression was some sort of integer and so that's the type of x
And so is this:
if keep { save_the_files() } // Otherwise I guess don't save them? Evidently save_the_files() doesn't return anything
Or this:
if keep { save_the_files(); } // save_the_files(); is a statement, if save_the_files() did return anything it's gone
But this won't compile:
let x = if even { 6 }; // If it isn't even what then? We need the implied else clause to type check and it does not
Posted Nov 1, 2023 10:34 UTC (Wed)
by farnz (subscriber, #17727)
[Link]
The other thing that I found helpful when trying to get used to lifetime annotations was to take my "normal" Rust code and apply the rules of lifetime elision to remove all the implied lifetimes. That way, I was able to build a sense of what the implied lifetimes were actually doing for me, and thus get to a point where when I needed to read annotations, I was used to them; it also meant that when I needed to write annotations, I had an intuitive sense built of what I was telling the compiler.
Bjarne Stroustrup’s Plan for Bringing Safety to C++ (The New Stack)
That's exactly how books teach Rust. It's the obvious way. And then they teach you about how to make it readable by removing all unnecessary lifetime annotations.
Therefore, it's obvious that most people don't know that concept, if they don't know Rust, yet. But it's actually pretty easy to learn. There are only a handful of rules to remember. And most of the time the compiler just inferres it for you. Which is a good thing, because otherwise Rust code would be completely unreadable.
Bjarne Stroustrup’s Plan for Bringing Safety to C++ (The New Stack)
Bjarne Stroustrup’s Plan for Bringing Safety to C++ (The New Stack)