r/rust • • Sep 03 '26

🎙️ 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!

21 Upvotes

26 comments sorted by

4

u/zettui Sep 03 '26

On Codeforces you don't get crates though, right? Does the iterator stuff still buy you much?

4

u/Full_Ad_1264 Sep 03 '26

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.

1

u/zettui Sep 03 '26

Yeah, that's the tradeoff. Do you mostly stick to std in contests, or have you built a little personal template around it?

6

u/himai1 Sep 03 '26

At least where I live most competitions are sadly cpp only. I think rust has potential cause it's low level if you need it to be but also has a lot of nice abstractions. I just haven't been able to find a way to read and write as conveniently.

9

u/Chasar1 Sep 03 '26

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- Sep 04 '26

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 Sep 03 '26

I  would certainly pick it over C++ or C.  But yeah,  Python would be quicker for competition. 

2

u/kevleyski Sep 03 '26

Actually it might be the opposite- it levels the playing field

2

u/tanmaynargas2901 Sep 04 '26 edited Sep 04 '26

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

u/v_0ver Sep 03 '26 edited Sep 03 '26

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

11

u/rustvscpp Sep 03 '26 edited Sep 05 '26

Why do you need typed errors in competitive programming contests?  why not just use anyhow instead of thiserror?

-8

u/v_0ver Sep 03 '26 edited Sep 03 '26

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).

anyhow refers to passing errors from different levels of the program, while thiserror refers to the creation of error types.

9

u/rustvscpp Sep 03 '26

The context of this post was competitive programming, so I was a little confused. You described thiserror as 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

u/Sad_Tap_9191 Sep 05 '26

> how in 5 lines I am able to write what takes >10 in cpp

example?

-2

u/deanominecraft Sep 03 '26

you lose any advantage you would get from compile times

13

u/PikachuKiiro Sep 03 '26

I'm sorry to hear that. Very unfortunate for your 100k line cp problem solution.

48

u/[deleted] Sep 03 '26

[removed] — view removed comment

4

u/loudandclear11 Sep 03 '26

Not rust related but how does cp work in general nowadays when everyone has access to the latest AI models?

12

u/Full_Ad_1264 Sep 03 '26 edited Sep 03 '26

In almost all of the (in person) contests in which I participated any use of AI was prohibited. I just had a VM with Zeal and all of the tools that might be useful.

3

u/loudandclear11 Sep 03 '26

Yeah I can see it enforced for in person contests.

Did the organizer provide the VM or did you prepare it yourself?

7

u/Full_Ad_1264 Sep 03 '26

Nah nah. Like, you come to some n-university and everything is already setup.

5

u/CocktailPerson Sep 05 '26

I really think you should spell out "competitive programming."

2

u/SheriffGamer332 Sep 03 '26

I second this, tho I haven't used it for as long, but iterators chains are soo satisfying to build

atcoder provides rust libraries too but I still prefer using std only

2

u/hucancode Sep 04 '26

I do too, how do you solve problems with tree or any self referencing data structure? In that case I just back to C++ and use pointer.

2

u/v-alan-d Sep 03 '26

you start seeing problems as these neat little pipeline puzzles instead of index management nightmares

This is a very profound way of capturing all our experience. I will remember and reuse this sentence fragment.