Je suis développeur web freelance et propose des formations à Symfony2 ! Contactez-moi pour en discuter.

I had a hard time learning rust at first. I didn’t quite know where to start, I focused on the wrong resources and lost some time. Here are my suggestions about everything you may need in order to learn and work with rust: where and what to learn, how to properly install the compiler, what tools you need and how to use them.

TL;DR:

  • Useful learning resources:
    • Start learning rust with the rust book. It will teach you why things work like this in rust, which is more important than how in this language (but you’ll learn that too). It’s free, online, and hands on: there are a few practicals apps that you’ll build while reading the book (the first is in chapter 2, almost right away).
    • You can share a code sample with your problem on rust playground] to rust-beginners.
    • Use Rust by example in order to refresh your memory.
    • Browse 24 days of rust. You’ll see some apps, patterns or crates (rust libraries)… from the rust ecosystem.
    • If you work for the web, http://www.arewewebyet.org/ provides info about the current state of the crates for web development.
  • Tools you may find useful:
    • rustup allows you to have multiple rust versions on your system. It’s handy when some crates require you to use a nightly build of the compiler.
    • racer will let you have code completion, and there are various plugins for your IDEs.
    • learn how to use a debugger like gdb or rust-gdb in order to debug you code
    • rustfmt lets you reformat your code in a standard way
    • clippy is a linter. It can help you spot bad code patterns.

Why sould you learn Rust ?

That’s not the topic of this article 🙂 I’ll assume you have a basic understanding of what it’s meant for, but are not quite sure where to get started.

Start with the rust book. Learn the WHYs, not just the HOWs.

I’ve learnt a lot of languages since I started programming, ~15 years ago. I’ve had the opportunity to use them both for personnal projects, out of curiosity, or in a professional setting, where the constraints are different. I guess I can consider myself an experienced developer.

Often, when it comes to learning a new language, things are easy: I simply pop a REPL or some sample hello world project, grab some documentation, and start messing around. I wrote a Brainfuck interpreter in Ruby a few hours after I started reading about the language, because the langage is very close to python, which I already knew.

Things in Rust didn’t work like that for me.

Like many, I tried to jump at writing code and faced a lot of walls. The compiler gives detailed explanations about where I did mistakes, but sometimes I didn’t understand what was wrong.

When learning rust, you don’t simply need to learn how to do things. You need to understand why you do them like this. And it will question a lot of your bad habits as a developer. Like using mutable variables when you don’t need to (it’s way more tricky than it sounds), extensively checking for errors (you really have no idea how terrible you are. Yes it’s hard, yes it’s not fun, but if you won’t to be in real control of what’s happening, yes, you should deal with all the errors your code may throw !), dealing with return values (not using one throws a warning in rust).

Rust enforces the principle of least privilege, and you’ll most likely notice (via trial and error) that it’s not something you’re used to. And that you don’t do it as much as you think you do. It’s pretty frustrating to feel like the code you usually write is bloated with mistakes, but this deeper understanding about how code should behave will help you and you code grow.

Then, go further

Once you understand why things work the way they do in Rust, it’s time to broaden your horizons.

  • Rust by example contains a lot of example, in order to quickly refresh my memory or to get started using a particular component.
  • 24 days of rust will make you discover apps, patterns or crates (rust libraries)… from the rust ecosystem.
  • If you work for the web, http://www.arewewebyet.org/ provides info about the current state of the crates you may need for web development.

Browsing through the code of popular crates will also help you understand some idiomatic code patterns. Plenty of them are on Github.

Asking for help

These resources cover a lot of ground but you may need help in order to understand some things. The Rust Playground will let you share and execute a code sample, so that’s easier for someone else to understand and debug or fix the piece of code you’re stuck with.

You can ask for help on stackoverflow, or Reddit but there is also the rust-beginners IRC channel on irc.mozilla.org. A lot of friendly people there.

Setting up a complete working environment

A small overview about how to properly install rust and some dependencies that can be useful.

Installing rust with rustup

Contrarilly to many tools, you don’t install the rust compiler right away when you get started with rust: instead, you first install rustup, a rust compiler version manager. That’ll allow you to install multiple versions of rust, and switch between them at will. Some libraries, like clippy (it’s a rust linter) expect you to use the nightly build, while most of the time, you may want to use the latest stable release, so you may need to be able to switch between language versions.

Once you have installed rustup, you can install a new build of the language. For instance, here I install the latest nightly build:

$ rustup install nightly
… installing info
$ rustup show
Default host: x86_64-unknown-linux-gnu
 
installed toolchains
——————–
 
stable-x86_64-unknown-linux-gnu (default)
nightly-x86_64-unknown-linux-gnu
 
active toolchain
—————-
 
stable-x86_64-unknown-linux-gnu (default)
rustc 1.18.0 (03fc9d622 2017-06-06)

You can see that I have two build toolchains setup: the stable and the nightly. The active one is the stable. So without more details, cargo run will execute thing with the active toolchain, but I can change it:

$ cargo run # runs the project with the active toolchain
$ cargo +nightly run # runs the project with the nightly
$ rustup default nightly # setups the nightly as active
$ cargo run # now my project will be run using the nightly build

Understand the existing tools

cargo

cargo is rust’s package manager. You’ll use cargo for a lot of things (on the rust book, for instance). Among other things, it can:

  • scaffold a new project:
    • library: cargo new some_project
    • binary: cargo new some_project –bin
  • build your project ($ cargo build)
  • run the project ($ cargo run)
  • install a dependency: cargo install some_project_or_library
  • run the unit tests: $ cargo test (the examples in the documentation can also be considered as unit tests! )
  • build and open the project’s documentation (it includes the doc for the dependencies): cargo doc –open

Thanks to cargo, you can easily get started with continuous integration of a platform like Travis-CI. For instance, you may want to run the tests every time a commit is pushed on a branch on github, in order to see if said branch breaks the project.

Travis-CI’s got some explanations regarding how to do that.

Debugging gdb, rust-gdb

In a complex project, you may need a debugger in order to understand why things do not work the way you thought they were (no, printing stuff to the console is NOT a good habit).

You can use gdb (or better, rust-gdb, in provides more context). Instead of running your project with

$ cargo run

you’ll do

$ gdb target/debug/name_of_you_executable

or

$ rust-gdb target/debug/name_of_you_executable

Then, the application will run inside the debugger and you see what is happening. Here are some more info about getting started, and here is an useful list of how to’s) to get you started with gdb.

Installing some more things

Out of the box, some things are missing: I’ll give you detail about how to properly and automatically format your code, how to lint and setup code completion.

Formatting the code with rustfmt

There is not rust fmt like there is in Go. I think go fmt is fantastic, because it puts code style of out the equation. I find the talks about code style useless and boring, and I’ve found go fmt to be an amazing way to get that out of the way: in Go, the code style is part of the language.

There is no such thing in rust, but there is rustfmt. It’s a tool that enforces a given code style.

$ cargo install rustfmt

The you can reformat a source file or directory:

$ cargo fmt src

Code completion with racer (with rustup for atom)

racer is a code completion tool. It’s super useful if you don’t want to have to browse the documentation every other second.

It relies on the rust source code to provide this information, so we need to download it before installing racer:

$ rustup component add rust-src
$ cargo install racer

Then, you’ll need to setup racer in your IDE. I use atom, and there is an atom-racer package.

If you use rustup, you’ll need to provide 2 information in the settings of the rust-atom package:

  • the location of the rustc binary
$ which rustc
/home/clemk/.cargo/bin/rustc

  • the location of the rust source code
$(rustc –print sysroot)/lib/rustlib/src/rust/src/

I’ve also had an issue with flickering in Atom, because of the frequent creation of a temporary file. I solved it thanks to this page. Doesn’t sound solid, but that’s OK for now.

Yeah, I know. There’s a kind of irony in working with a crazy quick language in a javascript-powered electron app, but hey, nobody’s perfect.

Conclusion

That’s it ! I hope this article gave you some ideas about where to get started learning rust, and what are the tools you may need. It’s a long road, but the landscape is worth it.

Répondre

Unable to load the Are You a Human PlayThru™. Please contact the site owner to report the problem.