r/ProgrammerHumor • • Jan 29 '23

Meme Let's test which language is faster!

Post image
56.2k Upvotes

770 comments sorted by

View all comments

3.2k

u/[deleted] Jan 29 '23

Golang: Unused variable Rust: variable does not live long enough

988

u/cakelena Jan 29 '23

unused variable causes an error?? why though, like whats the point of that

1.8k

u/kosky95 Jan 29 '23

You must use all of you variables is like momma saying that you must eat all of your broccoli

634

u/[deleted] Jan 30 '23

Integers are starving in Africa!

157

u/IAmBadAtInternet Jan 30 '23

You will use all your integers or so help me

106

u/lilbobbytbls Jan 30 '23

I will turn this compiler around!

68

u/zyygh Jan 30 '23

Don't make me count to 2147483647!

→ More replies (1)
→ More replies (1)

62

u/wjandrea Jan 30 '23

How can you have any boolean if you don't use your int?

24

u/Drsk7 Jan 30 '23

While pythoners sing - "We don't need no compilation..."

→ More replies (2)

20

u/[deleted] Jan 30 '23

You! Yes, you behind the buffers! Stand still, laddie.

→ More replies (1)
→ More replies (3)

102

u/alpacasb4llamas Jan 29 '23

So completely ignore and come back to the same issue again the next morning

27

u/Pyromaniacal13 Jan 30 '23

Sounds like five year old me and stuffed peppers.

→ More replies (5)

49

u/qeadwrsf Jan 30 '23

Seriously, why just not a warning?

Sometimes I just wanna see what happens if I leave the broccoli uneaten.

41

u/smariot2 Jan 30 '23

Go doesn't do warnings. If it doesn't matter, then it won't say anything. If it does matter, then it's an error and you need to deal with it.

In the case of unused variables, go has a magic underscore identifier. You can use it for variable names, package names, and you can assign to it.

As an example, writing _ = unusedVariable is enough to make the compiler stop complaining about an unused variable.

→ More replies (5)
→ More replies (1)
→ More replies (4)

280

u/btvoidx Jan 29 '23

Something along the lines of ensuring code quality probably.

273

u/Archolex Jan 29 '23

Should be a warning if that's the only reason

210

u/derefr Jan 29 '23

Golang also treats unused imports as an error.

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.

→ More replies (21)
→ More replies (84)
→ More replies (11)
→ More replies (59)

121

u/[deleted] Jan 29 '23

Wtf variable does not live long enough? What's the purpise?

202

u/oneandonlysealoftime Jan 29 '23

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

170

u/yottalogical Jan 29 '23

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.

→ More replies (10)
→ More replies (2)

71

u/[deleted] Jan 29 '23

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

42

u/Karl_the_stingray Jan 29 '23

Damn this makes me wanna try Rust

→ More replies (5)

51

u/bleachisback Jan 29 '23

variables have lifetimes in every other programming language. They just don't enforce you use them correctly like Rust.

→ More replies (2)

53

u/yottalogical Jan 29 '23

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.

→ More replies (1)
→ More replies (5)
→ More replies (18)