Your submission was sent successfully! Close

Thank you for signing up for our newsletter!
In these regular emails you will find the latest updates from Canonical and upcoming events where you can meet our team.Close

Thank you for contacting our team. We will be in touch shortly.Close

  1. Blog
  2. Article

Ed Jones
on 25 July 2023

Rust has been the language most loved by developers for the last 8 years and it is seeing increased adoption by software companies of all sizes. However, its many high-level rules and abstractions create a steep initial learning curve which can leave the impression that Rust is the preserve of a select few but this couldn’t be any further from the truth. Let’s examine why Rust is often a great choice and take a look at how to get started with it on Ubuntu.

What’s the fuss all about?

Rust is a language with many exciting properties, including excellent security, high-level ergonomics, low-level performance and a superb user experience. So how do these make Rust the right choice for you?

It’s no secret that Rust is a secure language, and much of this is down to its expressive type system which allows for rigorous checks during compilation. Put simply, if a Rust program passes compilation, memory usage is guaranteed to be safe, resources cannot be used after being released and errors have been handled or acknowledged. Since memory errors are the source of most security vulnerabilities recently found by Microsoft, Google and Apple, these safety features mean that the language is uniquely placed to help create reliable and secure software.

Writing Rust, although daunting given its initial learning curve, is actually quite pleasant. The language provides many high-level abstractions, such as a standard container library, iterators, traits and functional programming style methods, all of which avoid the need to repeat common implementation boilerplate, allowing the developer to focus on implementing useful features. Rust’s strict type system helps significantly with this, reducing the time spent chasing bugs with debugging tools to the point that, as Andre Bogus, a maintainer of Rust’s linter puts it, ‘applications often don’t need debugging in the first place.’

In other languages, pleasant high-level abstractions would incur a significant cost, but in Rust, it is possible to have these benefits and the performance normally reserved for low-level languages, something which is possible for two reasons. First, Rust builds on twenty years of cutting-edge research on LLVM. As such, its human-friendly constructs can be optimised away at compile time, leaving behind only machine-friendly instructions in binaries. Second, Rust’s type system introduces the concepts of ownership and borrowing. These allow the compiler to insert instructions to free resources when they go out of scope. As a result, Rust programs have extremely low memory usage, especially when compared to garbage-collected alternatives. This great emphasis on low-level performance results in efficient binaries which are kinder to the user’s system, wallet and environment.

Clearly, Rust has excellent foundations for making reliable and efficient software, but without a compelling user experience, these benefits could struggle to leave the ivory tower. Here lies Rust’s secret—its best-in-class user experience. From straightforward setup (as will be seen below) to superbly helpful tooling and documentation, all elements of the Rust experience have been meticulously thought out. Its error messages annotate the source to show where and how problems originate, with more detailed explanations available by just asking the compiler. Its linter provides clear, idiomatic advice and can often automatically fix the problems it detects. Its documentation has a characteristic quality, containing plenty of explanations and examples to ensure the user knows exactly where, how and why to use any given construct. In Rust, knowledge is shared, not hoarded.

These features make Rust an excellent choice to learn and use in all fields, from the kernel and embedded systems, to the desktop, the cloud and the web. To the new user, it is quickly apparent that a great deal of care, love and attention has been put into this language. For now though, let’s briefly look at how to get started and settled into Rust.

Bootstrapping a new Rust installation

For this section, you will need: Ubuntu

It is recommended to install Rust through rustup, the Rust toolchain version manager. To install rustup and then install the latest stable Rust release, run the following commands.

curl --proto '=https' --tlsv1.2 -sSf | sh
rustup toolchain install stable

Next, let’s create, build and run our first project by running the following in a new directory.

cargo new hello-world
cd hello-world
cargo run

And that’s it! Beneath the compiler output, you should see “Hello, world!”, meaning you’ve just successfully built and run Rust code. Welcome to the world of Rustaceans!

Now you have a working compiler, let’s set up a nice environment, ready for productive Rust-writing.

Bootstrapping a new Rust dev environment

For this section, you will need: VSCode

A great Rust experience can be had through VSCode, the experience out of the box is okay, but let’s tweak things to make it really nice. 

First up, the most important tool for writing Rust is the language server, found in the rust-lang.rust-analyzer extension. This will provide many hints and suggest lint fixes from within the editor. For the best suggestions, change the ‘check’ command setting from check to clippy. (The former checks types, whereas the latter checks types, idiomatic use, code correctness and much more!)

It is useful to be able to pause a program mid-execution to inspect the values it’s currently working on. Install vadimcn.vscode-lldb to add a debugger for Rust, allowing you to set breakpoints and look at your program’s current state.

A core part of writing Rust is using the surrounding ecosystem effectively. To help with package management, install serayuzgur.crates. This will easily allow you to keep dependencies up to date by suggesting versions.

Finally, writing Rust occasionally requires editing Cargo.toml, the package manifest. For this, tamasfe.even-better-toml provides a good experience. This improves default syntax highlighting and validation.

Bootstrapping a new Rustacean

For this section, you will need: yourself 🙂

There are plenty of resources to help with learning Rust and here are a few suggestions.

To get going quickly, check out the Tour of Rust for many focused, interactive exercises to get you started quickly. The Tour makes no assumption of previous knowledge and is available in many languages.

Every Rustacean should take a look at the official Rust Book. It contains a wealth of explanations and examples designed to help you understand the building blocks of all Rust programs. As such, it provides a one-stop shop of tutorials and reference materials. The Book, like the Tour, assumes neither prior knowledge nor language.

For something more concise, try Rust by Example provides an ultra-concise view of the Rust language.

To really get to grips with the tooling and try out the language on your own systems, try the fantastic Rustlings course.

Last, but by no means least, if you ever have any problems or you just want to find more Rustaceans, join Rust’s large, passionate and welcoming community on one of their many platforms.

More Rust news to come

This article is the first in a series on Rust developments at Canonical. Over the coming weeks and months, we’ll be shedding light on some exciting developments as we strive to integrate Rust into both Ubuntu and our systems. Stay tuned for more!

Related posts

26 February 2024

Crafting new Linux schedulers with sched-ext, Rust and Ubuntu

Ubuntu Article

In our ongoing exploration of Rust and Ubuntu, we delve into an experimental kernel project that leverages these technologies to create new schedulers for Linux. Playing around with CPU scheduling policies has always been a dream for many kernel hackers and OS enthusiasts. However, such material typically remains within the domain of a fe ...

Felicia Jia
18 June 2024

Empowering RISC-V with open source through Ubuntu

Silicon Article

Canonical collaborates with partners to deliver optimised Ubuntu on RISC-V platforms, empowering innovation on RISC-V  Open source and global standards have a long history of success because they have a license framework that ensures anyone, anywhere can have ongoing access to them. RISC-V,  an open standard Instruction Set Architecture ( ...

Andreea Munteanu
17 June 2024

Top 5 reasons to use Ubuntu for your AI/ML projects

AI Article

For 20 years, Ubuntu has been at the cutting edge of technology. Pioneers looking to innovate new technologies and ideas choose Ubuntu as the medium to do it, whether they’re building devices for space, deploying a fleet of robots or building up financial infrastructure.  The rise of machine learning is no exception and has encouraged ...