Why would I solve LeetCode problems in Rust?! I wanted to learn Rust, without committing myself to some large real-world project. LeetCode monthly challenges are good for consistently practicing.
Why would someone not use Rust for LeetCode problems? Mainly because of the learning curve. It's a little difficult to successfully run a Rust program in one go unlike other common languages. You don't want to fight the borrow checker in a contest. Also, C++/Java/Python/JavaScript are more expressive to quickly solve these problems.
Most of the solutions here are self contained programs. I did not use cargo because I wanted to keep it real simple.
You can copy paste a code and run it online in Rust Playground.
To run locally:
rustc 1234-some-file.rs -g -o run
./run
Run the default build task to compile and run:
- Ctrl+Shift+p
- Select: "Tasks: Run build task".
Pro tip: Assign a keyboard shortcut to the default build task. I use Ctrl+Shift+B. Set the backtrace level in tasks.json, if needed.
There is also a "test" task for formatting the code with rustfmt
. I've assigned it to Ctrl+Shift+V.