Re:Best aspects of C
by arcticbull on Tuesday November 17, 2020 @04:15PM
(#25129949)
You don't need to specify the lifetime annotations on print_refs because the references aren't returned and are unambiguously just the lifetime of the function. The compiler will fill those in for you -- even if the lifetimes of x and y are different, just so long as x and y live at least as long as the function.
fn print_refs(x: &i32, y: &i32) {
println!("x is {} and y is {}", x, y);
}
In general you only need to specify lifetime parameters when there's an ambiguous situation. For instance, the following builds and runs just fine.
I'll be the first to admit it takes a while to ramp up into Rust. Part of that is learning to let go and trust the compiler. Unlike many other languages, it won't hurt you haha. It's on your side. Sometimes you need to you know, give it a little more info so it can do it's job.
The Rust team has made huge strides in improving writability of the language, especially with non-lexical lifetimes.
Soon, `fn` and `impl` and `Vec` fade into the background, like `char` and `short` and `long`.
The Rust team has made huge strides in improving writability of the language, especially with non-lexical lifetimes.
Soon, `fn` and `impl` and `Vec` fade into the background, like `char` and `short` and `long`.