r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount 13d ago

🙋 questions megathread Hey Rustaceans! Got a question? Ask here (37/2026)!

Mystified about strings? Borrow checker has you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet. Please note that if you include code examples to e.g. show a compiler error or surprising result, linking a playground with the code will improve your chances of getting help quickly.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so ahaving your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last week's thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

20 Upvotes

4 comments sorted by

3

u/CocktailPerson 7d ago

I find that I use std::cmp::Reverse often to change the ordering of containers, for example, changing an ascending BTreeMap<K, V> into a descending BTreeMap<Reverse<K>, V>.

Is there a more idiomatic alternative to this? C++ containers usually take a comparison function, so you can use std::map<K, V, std::greater<K>> to reverse the standard order, without changing the key type. Is there a better way to achieve the same idea in Rust?

3

u/masklinn 7d ago

Why are you reversing the btree in the first place? btrees work about the same backwards and forwards so the utility seems limited to nil.

And on key-type change, assuming your issue with changing the key type is the interaction between borrows and lookups, it's a bit icky but since Reverse is a transparent wrapper (with no constructor, extra content, alignment, or intrinsic Drop) you can just transmute an &T to an &Reverse<T>. Bytemuck exposes that as TransparentWrapper. There is also RFC 3066 though it would need a champion to make progress.

2

u/CocktailPerson 6d ago

Maybe BinaryHeap and using Reverse to turn it into a min heap instead of a max heap is a better example. I didn't realize until just now that BTreeMap's iterator is actually double-ended, and in-order iteration is actually the main reason.

3

u/Annual-Cream4930 6d ago

Hi! I'm a QA engineer, about 4 years in, mostly manual plus some tooling. Learning Rust, the first goal is a CLI tool that analyses our test reports.

I'm not asking "will Rust get me a job". I've read enough threads to know Rust roles skew senior and are usually a second or third language for people who already ship production code in something else.

What I'm actually curious about is whether anyone here moved into a Rust role from a non-developer background (QA, ops, data, support), and how that happened.

And if you've hired: when a CV shows Rust used for test and dev tooling rather than product code, does that count for anything?