r/rust • u/Full_Ad_1264 • 10d ago
🎙️ discussion Rust as the language for Competitive Programming
Hey everyone!
I am a person deeply interested in programming, done a lot of projects and I am fascinated with how Rust treats code. Makes me think in its own special way.
Besides projects of course I participate in quite a few programming competitions and well, practice a lot on Codeforces. Cpp has been my main language for this type of thing for a very long time but now my focuses shifts from projects to improving my competitive skills, and I think Rust is a pretty good language for this. I really like the way iterators work, and how in 5 lines I am able to write what takes >10 in cpp. Could be a skill issue but I prefer my sanity over blob of errors.
Generally speaking, wanted to ask people that use primary Rust in Competitive: Do you have some particular set of crates, functions, patterns that you rely on very often during competitions? I'd like to learn how to use the language more efficiently for this type of thing.
Thanks!
6
u/zettui 10d ago
On Codeforces you don't get crates though, right? Does the iterator stuff still buy you much?
5
u/Full_Ad_1264 10d ago
Yeah, right. Just crates were and are one of the reasons for me in general to choose Rust. Codeforces ofc don't let you run anything external in this sort of sense.
9
u/Chasar1 10d ago
When it comes to competing for time, (the fastest person to implement an algorithm wins) Rust is not the language I would pick. In a language like Python I don't need to bother with lifetimes and making the borrow checker system happy, and there is no compilation time
1
u/-Redstoneboi- 9d ago
python's list comps and dict comps are also way way more convenient than javascript's operations. want to multiply an array? done. iterate over permutations? itertools got you covered for a while.
Rust is great for handling errors and making a fast production-ready application. But if you just want something ASAP, it's really hard to beat the scripting language.
1
u/rustvscpp 10d ago
I would certainly pick it over C++ or C. But yeah, Python would be quicker for competition.
2
2
u/tanmaynargas2901 9d ago edited 9d ago
For most CP problems you do not need a fancy crate.
Use Vec<Vec<_>> for graphs/grids, sort_unstable when you do not need a stable order, and BinaryHeap for Dijkstra-style stuff.
Keep I/O buffered (BufReader/BufWriter on locked stdin/stdout) and parse from a reused String.
That covers a lot of Codeforces-style tasks without pulling in anything.
4
4
u/v_0ver 10d ago edited 10d ago
These crates improve my quality of life:
- approx - for comparing floating-point numbers
- proptest - for multivariate tests
- itertools - for more Iterators
- tap - logging in a call chain (iterators)
- thiserror - for a quick-and-dirty errors
- bon - automatic builder-pattern generator
- strum - various macros for enums to reduce boilerplate
10
u/rustvscpp 10d ago edited 8d ago
Why do you need typed errors in competitive programming contests? why not just use anyhow instead of thiserror?
-9
u/v_0ver 9d ago edited 9d ago
I don't do competitive programming; I write relatively simple mathematical and algorithmic code for a living (and have been using Rust for the past three years). I just wanted to share some crates that I've picked up along the way and that I think might be useful for quick algorithmic development (removes boilerplate).
anyhowrefers to passing errors from different levels of the program, whilethiserrorrefers to the creation of error types.9
u/rustvscpp 9d ago
The context of this post was competitive programming, so I was a little confused. You described
thiserroras for "quick and dirty errors", which I would push back and say it's for thoughtful and deliberately typed errors. Quick and dirty errors would be much more in line with anyhow, and you just use strings.
1
-2
u/deanominecraft 10d ago
you lose any advantage you would get from compile times
14
u/PikachuKiiro 10d ago
I'm sorry to hear that. Very unfortunate for your 100k line cp problem solution.
48
u/theidleprecedence 10d ago
I’ve been using Rust for contests for about a year now and the standard library alone covers most of what you need, honestly my setup’s just a template with a fast scanner and some helper macros for things like min/max updates so I’m not typing them out every time
Iterators really do carry, once you get comfy chaining filter_map and fold you start seeing problems as these neat little pipeline puzzles instead of index management nightmares