Programming, Technology

Rust Language Environment

Rust is a rather young language getting more and more popular as a “safe, concurrent, practical language”. I wanted to learn how to code in rust, and I wanted to setup a development environment to make the learning path easier.

Here are the notes on how I set up my Rust + Visual Studio Code environment. I wrote them while setting them up on a Ubuntu Linux 16.04. It might work on other platforms too. I tried to do it on Windows, but it is slower and it ends up being a pain, especially when it comes down to debugging (rust-lldb is not available on the msvc toolchain) [EDIT: apparently someone had more chance than me with this environment].

  • Download and run the rust installer called rustup: curl https://sh.rustup.rs -sSf | sh
  • I chose to install the nightly toolchain, because it has already happened to that some packages work only on the nightly toolchain and they produce errors on the stable one (sigh).
  • Once everything is installed and you have the PATH correctly set up, you should be able to run for example the rust compiler (rustc) and the package manager (cargo) from your shell (installed on ~/.cargo/bin). To check if everything went fine, you can try to make a hello world project:
    cargo new hello_world --bin
    cd hello_world
    cargo run
  • In order to debug your binaries, you might want to install LLDB. To do that just type sudo apt install lldb. Another tool you might need is python’s six, you can install via pip install six. Once these steps are done you should be able to debug your binaries (try doing rust-lldb target/debug/hello_world)
  • Whatever your choice of IDE/editor will be, there are few tools that might help you when working on Rust code. Some of the tools this plug-ins might benefit from are racer, rustsym, rustfmt and RLS:
    • Racer is a code completion helper. To install it, you can use cargo: cargo install racer (it might take few minutes to download and compile all dependencies).
    • Rustsym is a tool to query symbols from rust code. To install it: cargo install rustsym
    • Rustfmt is a code formatter. To install: cargo install rustfmt
    • RLS is a language server. That means Rust compiler can run in the background, while the IDE might ask it information about the code you are writing. To install it, you can follow the information on their website, that sums up to this:
      rustup component add rls --toolchain nightly
      rustup component add rust-analysis --toolchain nightly
      rustup component add rust-src --toolchain nightly

I found that Visual Studio Code, together with few plug-ins, make a nice IDE. There might be alternatives (IntelliJ IDEA, Eclipse or Vim) but I was happy with VSCode. To install it, download it from their site. VSCode by itself will not be enough until you install some handy plugins:

  • VSCode-Rust to have the great rust support and turn VSCode into a good Rust IDE.
  • LLDB Debugger provides a nice interface with the debugger.

I had a silly message telling me that it could not detect the default toolchain, you just close it will propose you one of the installed toolchains.
Once all these tools are installed, you should have a nice IDE to work with Rust. To be honest, for some trivial changes I still go back to vim and the shell sometimes, but having an IDE makes life simpler most of the time, especially when you are a newbie at it.