I wanted a setup that made it as easy to work with Rust as it is to work with frontend development using npm, webpack and eslint. Here's a few utilities I put together to achieve that.
- Rust itself comes with a compiler and a package manager named Cargo (think npm). Cargo lets you easily install Rust packages by running
cargo install <package>
in the terminal. If you want a full intro to the basics of Rust, check the Getting Started page. - Racer provides code completion and should be hooked into your editor.
- cargo-watch lets you watch your project for changes so that you don't have to manually trigger a build to see if you code compiles every time you save a document.
- rustfmt helps you format Rust code according to style guidelines (think eslint), and you can either hook it into your editor or run it in the terminal.
- dybuk makes the terminal output nicer when compiling Rust. Unfortunately, though, it doesn't work together with rustfmt, so you can't use both at the same time.
- Install Rust
- Add a Rust plugin to your editor/IDE.
- Add
~/.cargo/bin
to your $PATH ― when you install packages withcargo install
, this is where the packages end up (at least on unix systems). If you don't add this directory to your $PATH, rustfmt, Racer and any other cargo packages will not be found.
- Install Racer with
cargo install racer
(may take a few minutes).- Also be sure to configure Racer or else it won't work (I suggest getting Rust sources by cloning the github repo).
- Add a Racer plugin to your editor/IDE.
- PS: If you see a bunch of temporary files with garbled names showing up when editing a Rust file, simply resarting the editor/IDE might fix it. If not, take a look at this.
- Install cargo-watch with
cargo install cargo-watch
(may take a few minutes). - Install rustfmt with
cargo install rustfmt
(may take a few minutes). - Install dybuk:
- cd to where you want to keep the dybuk repo
- Run
git clone https://github.com/ticki/dybuk.git
- Run
cargo install --path dybuk
- Having installed Rust you can run
cargo build
to build your project andcargo run
to build and run it. - With Racer and editor/IDE plugin installed, you should get code completion in your editor. Open src/main.rs in your editor and type in
hello::
, and you should get a suggestion to use the functionhello::print_hello()
if it is set up correctly. - Use cargo watch by running
cargo watch
in your project directory to watch for changes. Whilecargo watch
is running, make a change in one of thesrc/
files and save, and you should see it update it's output. - Use rustfmt by running
cargo fmt
. You can also use rustfmt with cargo-watch by runningcargo watch [test] fmt
. Note that if you want to configure rustfmt's write mode when usingcargo watch
you must do so in arustftm.toml
file, as the --write-mode flag would then be interpreted by rust-watch instead of rustfmt. In this project there rustftm.toml sets the write mode todiff
instead the defaultreplace
. - Use dybuk by appending
|& dybuk
when building or watching, respectively:cargo build |& dybuk
orcargo watch |& dybuk
. Unfortunately it doesn't work withcargo watch test fmt
because dybuk ignores the output of fmt.
If you want to use both rustfmt and dybuk, there's a few ways you can do that:
- Have to terminals for running
cargo watch |& dybuk
andcargo watch fmt
. Then you get prettyfied build output in one and rustfmt output in the other. - Run
cargo watch |& dybuk
while developing. Then runcargo fmt --watch-mode=replace
. (--watch-mode=replace
is the default, so you only need to add it if you want to override the setting fromrustfmt.toml
). Personally I'dgit add -A
all the changes first so I cangit diff
to see what changes rustfmt is making.
Now you're ready to start hacking! Check out the Rust guessing game tutorial if you need some inspiration, or go straight to the Syntax and Semantics.
If you have any feedback og suggestions for how to get a better (or maybe just different?) setup, let me know.