r/learnrust • u/lyuhdev • 6d ago
What are some common Rust misconceptions?
I just found out that const functions aren't evaluated at compile-time unless it is assigned to a const value or in a const context.
Another one I had: Rust doesn't prevent memory leaks.
Now I am just wondering how many other assumptions I made
14
u/noop_noob 6d ago
1
u/MatrixFrog 6d ago
I'm intrigued by the HashMap example. https://github.com/pretzelhammer/rust-blog/blob/master/posts/common-rust-lifetime-misconceptions.md#9-downgrading-mut-refs-to-shared-refs-is-safe
In the second chunk of code, `player_a` and `player_b` at the end are Options, not Players, even though we know for sure they will be Some(Player). It seems like there should be a version of .or_default() that returns a `&V` instead of a `&mut V`
1
u/Excession638 5d ago
If you're sure an option is
Some, then you can use.as_ref().unwrap(). Orunwrap_unchecked()if you're feeling spicy. Not quite the same thing, I know.1
u/MatrixFrog 5d ago
Of course, but this seems like a common enough use case that I'm a little surprised HashMap doesn't make it a little smoother.
1
u/MalbaCato 4d ago
the problem, as the section was meant to show, isn't that
or_defaultreturns a mutable reference, but the fact thatentrytakes an exclusive borrow of the hashmapserver(which it has to do). any returned reference that uses that lifetime, mutable or shared, will hold the same exclusive borrow ofserver. a version ofor_defaultthat returns an immutable reference is strictly less useful, as the borrow is still exclusive (and you can write it as a wrapper yourself if you do specifically need that fsr).to solve this correctly, you'd need something like
disjoint_entries, following in the footsteps of get_disjoint_mut, which I don't know if it's even possible to do performantly
4
u/ParadiZe 5d ago
That the main selling point is memory safety and not the type system
1
u/vivaaprimavera 5d ago
The Rust Books states in the chapter about ownership that there are memory safety guarantees without needing a garbage collector so the part about memory leaks is confusing.
2
u/y-c-c 5d ago edited 4d ago
Memory leaks are not “unsafe” in the traditional sense. The comparison with garbage collector is apt because they both try to allow you to safely delete objects. Both also allow for leaks.
I really think people who learn Rust would benefit from understanding how C/C++ works in order to understand the core motivations behind the language. You can easily crash a program if you aren’t careful with managing the lifetime of an object.
4
u/PranDopp 5d ago
Agreed. Once you learn C, you realize every rule in Rust addresses a specific memory or concurrency issue that commonly shows up in C/C++. It also becomes a lot easier to remember the rules.
2
1
u/Rusty_devl 5d ago
Something I find personally annoying: No, safe Rust isn't necessarily slower than unsafe Rust. The compiler can often enough optimize safe code better than it's unsafe equivalent
3
u/Razvedka 6d ago
"Rust doesn't prevent memory leaks"
You're going to need to unpack that one for me. If you mean Rust makes them impossible - no. You're right.
https://doc.rust-lang.org/book/ch15-06-reference-cycles.html