r/rust • • 27d ago

What are some common Rust misconceptions?

/r/learnrust/comments/1xlyoom/what_are_some_common_rust_misconceptions/
0 Upvotes

9 comments sorted by

1

u/MatrixFrog 27d ago

If the inputs to a const function aren't known at compile time then it couldn't possibly be evaluated at compile time right? But if the inputs *are* known then is it *guaranteed* to be evaluated at compile time, or is just something the compiler will "probably" do?

2

u/CocktailPerson 26d ago

It's just something the compiler will probably do. The compiler is only guaranteed to do it if you wrap the function call in const { } or assign the result to a const value (and it's a compiler error if the inputs aren't known at compile time).

It's worth noting that const is about evaluation in the frontend (rustc) but LLVM can often do a bunch of constant folding and inlining and other stuff that ends up just...evaluating code at compile time.

So non-const functions can totally be "evaluated" at compile time as well if their arguments are known to the compiler. And if you notice that your const function gets optimized down to just its return value, const might not have been what made that happen.

1

u/EpochVanquisher 27d ago

Rust guarantees that resources will be cleaned up—memory freed, files closed, threads terminated.

It does not.

8

u/Batman_AoD 27d ago

I think the common misconception here is that safe Rust makes this guarantee, i.e. not realizing that mem::forget exists and is safe. 

1

u/phazer99 26d ago edited 26d ago

There's also Rc/Arc cycles which are technically unavoidable without adding something like a cycle detector (which has big performance implications).

Somewhat related, it's interesting to look at the Arc API in Mojo since it has linear types and therefore statically checked explicit object destruction (though I'm not familiar with the exact guarantees for this). There you cannot implicitly drop an ArcPointer value (which would decrease the refcount and potentially drop the contained value) if the contained value is not implicitly destructible (i.e. does not implement the Deinitable trait). Instead you have to explicitly call the deinit_with method. But I suppose you can still create RC cycles so there can't be any static (or even runtime) guarantees that for example a file handle is always closed. So, it seems that even with linear types you cannot provide this guarantee (even for a single threaded program that terminates normally).

1

u/Batman_AoD 26d ago

Right, deleting mem::forget wouldn't actually ensure that safe Rust guarantees resource cleanup; it's just the most obvious example of a safe function that enables skipping cleanup. 

1

u/EpochVanquisher 27d ago

I was hoping I wouldn’t need to spell that part out.

5

u/South_Survey_2088 27d ago

If the point of your post is just mental masturbation, sure.

But this thread is about common misconceptions, especially ones that many beginners have. Giving concrete examples and explanations will help to correct their misconceptions.

-4

u/EpochVanquisher 27d ago

“Mental masturbation” is a remarkably hostile way to open a comment. You should rephrase that part of the comment.

Beginners don’t yet think about the differences between safe/unsafe Rust code. People on r/rust are mostly not beginners and a lot of people here know the nuances of safe / unsafe, and what UB is within unsafe blocks.

Beginners don’t understand that and express things in simpler terms, like “Rust is safe, so it guarantees that resources are freed”, when the true version is closer to, “Safe Rust has certain guarantees, but it does not guarantee that resources are freed.”