Americas

  • United States
sandra_henrystocker
Unix Dweeb

Why Linux users should try Rust

Reviews
Sep 24, 20185 mins
Linux

Installing the Rust programming language on your Linux systems could turn out to be one of the more rewarding things you've done in years.

Rust is a fairly young and modern programming language with a lot of features that make it incredibly flexible and very secure. It’s also becoming quite popular, having won first place for the “most loved programming language” in the Stack Overflow Developer Survey three years in a row — 2016, 2017, and 2018.

Rust is also an open-source language with a suite of special features that allow it to be adapted to many different programming projects. It grew out of what was a personal project of a Mozilla employee back in 2006, was picked up as a special project by Mozilla a few years later (2009), and then announced for public use in 2010.

Rust programs run incredibly fast, prevent segfaults, and guarantee thread safety. These attributes make the language tremendously appealing to developers focused on application security. Rust is also a very readable language and one that can be used for anything from simple programs to very large and complex projects.

Rust is:

  • Memory safe — Rust will not suffer from dangling pointers, buffer overflows, or other memory-related errors. And it provides memory safety without garbage collection.
  • General purpose — Rust is an appropriate language for any type of programming
  • Fast — Rust is comparable in performance to C/C++ but with far better security features.
  • Efficient — Rust is built to facilitate concurrent programming.
  • Project-oriented — Rust has a built-in dependency and build management system called Cargo.
  • Well supported — Rust has an impressive support community.

Rust also enforces RAII (Resource Acquisition Is Initialization). That means when an object goes out of scope, its destructor will be called and its resources will be freed, providing a shield against resource leaks. It provides functional abstractions and a great type system together with speed and mathematical soundness.

In short, Rust is an impressive systems programming language with features that other most languages lack, making it a serious contender for languages like C, C++ and Objective-C that have been used for years.

Installing Rust

Installing Rust is a fairly simple process.

$ curl https://sh.rustup.rs -sSf | sh

Once Rust in installed, calling rustc with the –version argument or using the which command displays version information.

$ which rustc
rustc 1.27.2 (58cc626de 2018-07-18)
$ rustc --version
rustc 1.27.2 (58cc626de 2018-07-18)

Getting started with Rust

The simplest code example is not all that different from what you’d enter if you were using one of many scripting languages.

$ cat hello.rs
fn main() {
    // Print a greeting
    println!("Hello, world!");
}

In these lines, we are setting up a function (main), adding a comment describing the function, and using a println statement to create output. You could compile and then run a program like this using the command shown below.

$ rustc hello.rs
$ ./hello
Hello, world!

Alternately, you might create a “project” (generally used only for more complex programs than this one!) to keep your code organized.

$ mkdir ~/projects
$ cd ~/projects
$ mkdir hello_world
$ cd hello_world

Notice that even a simple program, once compiled, becomes a fairly large executable.

$ ./hello
Hello, world!
$ ls -l hello*
-rwxrwxr-x 1 shs shs 5486784 Sep 23 19:02 hello		

And, of course, that's just a start — the traditional "Hello, world!" program. The Rust language has a suite of features to get you moving quickly to advanced levels of programming skill.

Learning Rust

rust programming language book cover No Starch Press

The Rust Programming Language book by Steve Klabnik and Carol Nichols (2018) provides one of the best ways to learn Rust. Written by two members of the core development team, this book is available in print from No Starch Press or in ebook format at rust-lang.org. It has earned its reference as "the book" among the Rust developer community.

Among the many topics covered, you will learn about these advanced topics:

  • Ownership and borrowing
  • Safety guarantees
  • Testing and error handling
  • Smart pointers and multi-threading
  • Advanced pattern matching
  • Using Cargo (the built-in package manager)
  • Using Rust’s advanced compiler

Table of Contents

The table of contents is shown below.

Foreword by Nicholas Matsakis and Aaron Turon
Acknowledgements
Introduction
Chapter 1: Getting Started
Chapter 2: Guessing Game
Chapter 3: Common Programming Concepts
Chapter 4: Understanding Ownership
Chapter 5: Structs
Chapter 6: Enums and Pattern Matching
Chapter 7: Modules
Chapter 8: Common Collections
Chapter 9: Error Handling
Chapter 10: Generic Types, Traits, and Lifetimes
Chapter 11: Testing
Chapter 12: An Input/Output Project
Chapter 13: Iterators and Closures
Chapter 14: More About Cargo and Crates.io
Chapter 15: Smart Pointers
Chapter 16: Concurrency
Chapter 17: Is Rust Object Oriented?
Chapter 18: Patterns
Chapter 19: More About Lifetimes
Chapter 20: Advanced Type System Features
Appendix A: Keywords
Appendix B: Operators and Symbols
Appendix C: Derivable Traits
Appendix D: Macros
Index

The Rust Programming Language takes you from basic installation and language syntax to complex topics, such as modules, error handling, crates (synonymous with a ‘library’ or ‘package’ in other languages), modules (allowing you to partition your code within the crate itself), lifetimes, etc.

Probably the most important thing to say is that the book can move you from basic programming skills to building and compiling complex, secure and very useful programs.

Wrap-up

If you’re ready to get into some serious programming with a language that’s well worth the time and effort to study and becoming increasingly popular, Rust is a good bet!

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.