Rust is not like other languages. It’s harder to pick up than, say, Python or JavaScript, and it introduces new concepts that demand a different way of thinking. As daunting as that sounds, learning Rust is worth your time no matter where you are in your career because it will make you better at writing in other programming languages.
This guide aims to help you navigate the Rust ecosystem and set up your dev environment. That way, you’ll have one less thing to worry about as you familiarize yourself with this challenging language.
This installation is for MacOS and linux users. If you want to install Rust on Windows, see Other Rust Installation Methods.
Rustup is the program you would use to install Rust, or to upgrade to a newer or downgrade to an older version. It’s comparable to pyenv
for installing Python versions and nvm
and n
for Node versions, except that it is an official part of the Rust project.
You should visit https://sh.rustup.rs first to check the bash script you’ll be running on your computer. Once you’ve done so, open a terminal and run the following command.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Answer 1
to proceed with the default installation. When it’s done, run source $HOME/.cargo/env
or start a new terminal to make the rustup
command available.
Let’s test the install with rustup --version
.
Besides rustup
, the default installation also installs cargo
and rustc
.
cargo
is the Swiss army knife of the Rust ecosystem, much like npm
for JavaScript projects. It’s used to install dependencies and manage the building, testing, running, and publishing of your projects. cargo
installs Rust packages, which are called crates,* from crates.io, the language’s package registry, but also from git or a local directory.
rustc
is the actual Rust compiler. Although it’s the most fundamental part of the toolchain, you won’t really have to deal with it very often while working on a Rust project. Most of the time, rustc
is invoked indirectly by the cargo build
or cargo run
commands.
You can run rustup show
to get an overview of your current Rust setup.
âžś rustup --version rustup 1.21.1 (7832b2ebe 2019-12-20) âžś rustup show Default host: x86_64-apple-darwin rustup home: /Users/ijager/.rustup stable-x86_64-apple-darwin (default) rustc 1.43.0 (4fb7144ed 2020-04-20) âžś cargo --version cargo 1.43.0 (3532cf738 2020-03-17)
A new update of Rust is released every six weeks. These updates include new language features, new syntax, and fixes. If you want to try out new language features sooner, the latest updates become available almost daily through the nightly builds. rustup
makes it easy to select a Rust build from a certain day.
The following command will install the latest nightly.
âžś rustup toolchain install nightly âžś rustup toolchain list stable-x86_64-apple-darwin (default) nightly-x86_64-apple-darwin
Now you have two Rust versions: stable and nightly. However, stable is still selected by default. To select the nightly compiler, change the default.
âžś rustup default nightly âžś rustc --version rustc 1.44.0-nightly (b2e36e6c2 2020-04-22)
If you need a specific nightly version:
âžś rustup install nightly-2020-02-20 âžś rustup toolchain list stable-x86_64-apple-darwin nightly-2020-02-20-x86_64-apple-darwin nightly-x86_64-apple-darwin (default) âžś rustup default nightly-2020-02-20 âžś rustc --version rustc 1.43.0-nightly (7760cd0fb 2020-02-19)
This gives us the closest build available, from Feb. 19.
To get rid of a specific toolchain, simply run:
rustup uninstall nightly-2020-02-20
And don’t forget to select a toolchain again.
rustup default stable
Below is a simple overview of the Rust toolchain and ecosystem.
Included in the toolchain are the following.
rustup
toolchain installerrustc
rust compilercargo
package managercrates.io
package repositorydocs.rs
crate documentationThanks to Cargo, making a “Hello, World’” example in Rust is almost too easy. Start a new project with the name hello_rust
using cargo new
.
âžś cargo new hello_rust Created binary (application) `hello_rust` package
This command creates a new project that is ready to go. Let’s test it.
âžś cd hello_rust âžś cargo run Compiling hello_rust v0.1.0 (/Users/ijager/hello_rust) Finished dev [unoptimized + debuginfo] target(s) in 1.22s Running `target/debug/hello_rust` Hello, world!
Change 'world'
to 'rust'
in src/main.rs
to see if you’re really compiling and running this program.
// src/main.rs fn main() { println!("Hello, rust!"); }
Of course, you also build and run it in separate steps.
âžś cargo build Compiling hello_rust v0.1.0 (/Users/ingmar/dev/rust/hello_rust) Finished dev [unoptimized + debuginfo] target(s) in 0.18s âžś ./target/debug/hello_rust Hello, rust!
Besides main.rs
, cargo
also generates Cargo.toml
when creating the project. This is the package.json
of Rust. It describes the current package and its dependencies.
[package] name = "hello_rust" version = "0.1.0" authors = ["Ingmar Jager"] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies]
Our standard “Hello, World” app does not have any dependencies yet. Let’s add the csv
crate.
[dependencies] csv = "1.1.3"
Next, add a piece of code from the csv
crate README page to main.rs
. It reads csv
data from stdin
and prints the parsed output.
// make csv crate available use csv; fn main() { println!("Hello, rust!!"); let mut rdr = csv::Reader::from_reader(std::io::stdin()); // Loop over each record. for result in rdr.records() { // An error may occur, so abort the program in an unfriendly way. // We will make this more friendly later! let record = result.expect("a CSV record"); // Print a debug version of the record. println!("{:?}", record); } }
You can test it easily by piping a two-line csv
string (header + data) directly into cargo run
.
âžś echo "City, Country\nDelft,The Netherlands" | cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.01s Running `target/debug/hello_rust` Hello, rust!! StringRecord(["Delft", "The Netherlands"])
You won’t always include crates from crates.io. Sometimes, the crates will be local, such as when testing your own library crates.
Make a library crate named hello_print
using cargo
.
➜ cargo new hello_print --lib Created library `csvprint` package ➜ tree csvprint csvprint ├── Cargo.toml └── src └── lib.rs
Instead of main.rs
, a library is created with lib.rs
. To this, add a simple function that says “hello” to its argument.
//lib.rs pub fn print_hello(something: &str) { println!("Hello, {}!", something); }
Next, add your new library crate to the Cargo.toml
of your hello_rust
app. You can use a relative path for this.
# Cargo.toml [dependencies] csv = "1.1.3" hello_print = {path="../hello_print"}
Finally, in main.rs
, import the library and call the only function in its API.
// make csv crate available use csv; // import the print_hello function from the hello_print crate use hello_print::print_hello; fn main() { print_hello("from"); let mut rdr = csv::Reader::from_reader(std::io::stdin()); // Loop over each record. for result in rdr.records() { // An error may occur, so abort the program in an unfriendly way. // We will make this more friendly later! let record = result.expect("a CSV record"); // Print a debug version of the record. println!("{:?}", record); } }
Note: It’s wise to import with use hello_print::print_hello;
so that you can call print_hello("from");
instead of hello_print::print_hello("from")
.
âžś echo "city, country\nDelft, The Netherlands" | cargo run Compiling hello_rust v0.1.0 (/Users/ingmar/dev/rust/hello_rust) Finished dev [unoptimized + debuginfo] target(s) in 0.40s Running `target/debug/hello_rust` Hello, from StringRecord(["Delft", " The Netherlands"])
The community is working hard to make software development using Rust easier and more convenient. There are extensions and plugins available for several editors, but only those for VSCode are officially supported.
For syntax highlighting and autocomplete, the two most promising options are Rust Language Service (RLS)
and rust-analyzer
. Interestingly, they work in completely different ways under the hood.
rust-analyzer
is more promising and poised to be the successor of RLS
. The first release of rust-analyzer
was announced just last month. It’s still under heavy development and not yet optimized for all use cases. But for small projects, it is already recommended.
Why not try both for yourself? It’s super easy to install them from the extension marketplace.
Better TOML is a VSCode extension you can install to make working with Cargo.toml
more efficient. Besides syntax highlighting, it also suggests newer available versions for your dependencies, which is very useful.
I hope this guide helps you get up to speed with Rust development. For a deeper dive into the language, I recommend checking out the Rust book and following Rust by example. Better yet, start writing your own project today!
Debugging Rust applications can be difficult, especially when users experience issues that are hard to reproduce. If you’re interested in monitoring and tracking the performance of your Rust apps, automatically surfacing errors, and tracking slow network requests and load time, try LogRocket.
LogRocket is like a DVR for web and mobile apps, recording literally everything that happens on your Rust application. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred. LogRocket also monitors your app’s performance, reporting metrics like client CPU load, client memory usage, and more.
Modernize how you debug your Rust apps — start monitoring for free.
Would you be interested in joining LogRocket's developer community?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowLearn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.
Bypass anti-bot measures in Node.js with curl-impersonate. Learn how it mimics browsers to overcome bot detection for web scraping.
Handle frontend data discrepancies with eventual consistency using WebSockets, Docker Compose, and practical code examples.
Efficient initializing is crucial to smooth-running websites. One way to optimize that process is through lazy initialization in Rust 1.80.
One Reply to "Getting up to speed with Rust"
I would replace `echo` with `printf` on above, as in some environment, echo needs some parameters to interpret \n.