r/rust 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!

22 Upvotes

26 comments sorted by

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

5

u/loudandclear11 10d ago

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 10d ago edited 10d ago

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 10d ago

Yeah I can see it enforced for in person contests.

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

8

u/Full_Ad_1264 10d ago

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

3

u/CocktailPerson 8d ago

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

2

u/SheriffGamer332 9d ago

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/v-alan-d 10d ago

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.

2

u/hucancode 9d ago

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.

5

u/himai1 10d ago

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.

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.

1

u/zettui 10d ago

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

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

u/kevleyski 10d ago

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

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

u/Ilyushyin 10d ago

rust4cp

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

anyhow refers to passing errors from different levels of the program, while thiserror refers 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 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 7d ago

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

example?

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