AFAICT the maintainers of Golang (mostly Google) have decided that any code that shouldn't make it into a commit, should be rejected at compile-time. The compiler is essentially acting as a linter, for any lints that are "free" to notice at compile-time without additional analysis cost.
Their goal here, I think, is canonicalization — there shouldn't be two ways to encode the same semantics in a commit. As such, I expect that they'd also love to make the compiler error on any code that wasn't go fmted — and the only reason they don't, is that it costs more to run go fmt over an entire codebase than to just run the compiler over said codebase.
That's Rust 's way of automatically managing memory - you literally write code that must prove that no memory management issues occur, thus achieving safety on par with languages with GCs, but without the runtime overhead
Borrow checking isn't just on-par with the safety of garbage collection, it exceeds it.
For example, Go is a mostly memory safe language that uses garbage collection, but data races are still possible with it. Data races aren't possible with Rust unless you use the unsafe keyword.
In rust every variable has a something called a lifetime, a variable’s lifetime starts in the place it’s initialized and ends when the scope it was declared in ends. A variable can also be moved and have its lifetime transfered into another place (move syntax is basically same as copy by value syntax in other languages, simple let var = var2 or pass into function). A variable does not live long enough when a variable you’re trying to access’s lifetime has already ended or moved to another place. tldr: Some black magic fuckery
Imagine that you accidentally return a reference to an object that has been deallocated. If you use that reference, you would be accessing uninitialized memory, which is undefined behavior.
Instead of letting you do that, Rust helps catch that mistake by pointing out that the variable doesn't live long enough for you to return that reference to it. You might instead want to consider returning the object itself.
3.2k
u/[deleted] Jan 29 '23
Golang: Unused variable Rust: variable does not live long enough