How to Compete in Rust

Правка en3, от EbTech, 2019-06-04 01:35:51

Having spent a number of Codeforces rounds practising the ins and outs of Rust, I think it's finally safe to encourage wider participation in the language! This guide is intended primarily for C++ users who may have taken interest in Rust, but weren't sure if it's feasible for use in timed contests. On Quora, I summarized why Rust is actually a practical choice for contests. So here I'll go over one of my Codeforces submissions in more detail, showing some tips along the way.

My solution to 1168C: And Reachability (bitmask DP) My solution to 1158D: Winding polygonal line (geometry)

Right away, you'll notice some differences from your typical C++ submission:

  • No self-defined macros. Rust has good macro support, but I find plain code to be clearer.

  • No global variables, except for constants. By scoping things appropriately, I don't have to worry about accidentally forgetting to reset data.

  • Very few mutable variables:

  • A Scanner struct, with a polymorphic next() function. It can read space-separated tokens of any type that implements the trait FromStr.

  • Output via a BufWriter. This is needed for speed, if you have to write a large number of lines. It automatically flushes when it goes out of scope, but you'll probably want to flush() manually on interactive problems!

  • A mix of imperative-style and functional-style constructions, depending on which is clearer

Reading a vector of floats, and rendering it immutable. ~~~~~ let v: Vec = (0..n).map(|_| scan.next()).collect(); ~~~~~ Reading a string consisting entirely of 0s and 1s: ~~~~~ let s: String = scan.next(); let v: Vec = s.chars().map(|ch| ch == ‘1’).collect(); ~~~~~

let s: String = scan.next();
let v: Vec<bool> = s.chars().map(|ch| ch == ‘1’).collect();

Reading a vector from standard input, and rendering it immutable.

One very interesting finding from my experience with Rust is that it "production-quality" code and "quick" code look much more alike than they do in C++. This is because Rust not only makes it harder to do things the wrong way, but it also makes it much easier to do things the right way. As a result, I naturally find myself coding in a better style, even under time pressure.

Overall, my solutions attain much fewer WA verdicts in Rust than they did in C++. Development time is sometimes more, sometimes less, but it gets better with practice. Try it out!

Теги rust, programing languages, quickstart

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en17 Английский EbTech 2019-07-03 21:50:21 19
en16 Английский EbTech 2019-06-25 12:31:38 122
en15 Английский EbTech 2019-06-05 20:42:16 182 Tiny change: ' major one.\n\n- A m' -> ' major one, and sufficient for contests.\n\n- A m'
en14 Английский EbTech 2019-06-04 11:20:54 99
en13 Английский EbTech 2019-06-04 09:50:28 80
en12 Английский EbTech 2019-06-04 09:29:22 249 Tiny change: ' time, we Copy elem by patter' -> ' time, we `Copy` `elem` by patter'
en11 Английский EbTech 2019-06-04 03:20:20 32
en10 Английский EbTech 2019-06-04 03:12:34 157 Tiny change: 'ubstantial, so it may no' -> 'ubstantial enough that it may no'
en9 Английский EbTech 2019-06-04 03:07:39 309
en8 Английский EbTech 2019-06-04 03:06:30 146
en7 Английский EbTech 2019-06-04 03:01:44 249 Tiny change: 'o must be explicit' -> 'o must be made explicit'
en6 Английский EbTech 2019-06-04 02:56:04 2 Tiny change: '~\nMy 1168D submissio' -> '~\nMy 1168C submissio'
en5 Английский EbTech 2019-06-04 02:55:14 1133 Tiny change: 'mplements `Iterator`, so I Goo' -> 'mplements the `Iterator` trait, so I Goo' (published)
en4 Английский EbTech 2019-06-04 02:28:35 1983 Tiny change: '4903799)\n[My solu' -> '4903799)\n\n[My solu'
en3 Английский EbTech 2019-06-04 01:35:51 1003
en2 Английский EbTech 2019-06-04 01:10:30 58
en1 Английский EbTech 2019-06-04 01:07:14 1742 Initial revision (saved to drafts)