Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Rust Quick Start Guide
Rust Quick Start Guide

Rust Quick Start Guide: The easiest way to learn Rust programming

eBook
€13.98 €19.99
Paperback
€24.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Rust Quick Start Guide

Getting Ready

In this guide, we're going to learn the basics of working with Rust, a systems-level programming language that has been making a name for itself over the last few years. Rust is a strict language, designed to make the most common errors impossible and less common errors obvious.

Being a systems-level language means that Rust is guided by the needs of low-level programs that don't have a safety net, because they are the safety net for higher-level programs. Operating system kernels, web browsers, and other critical pieces of infrastructure are systems-level applications.

This is not to say that Rust can only be used for writing critical infrastructure, of course. The efficiency and reliability of Rust code can benefit any program. It's just that the priorities for higher-level code can be different.

In this chapter, we're going to cover the following topics:

  • The rustup tool
  • The cargo tool
  • How to start a new Rust project
  • How to compile a Rust project
  • How to locate third-party libraries
  • How to manage dependencies
  • How to keep a Rust installation up-to-date
  • How to switch between stable and beta Rust

Installing Rust

Installing Rust on any supported platform is simple. All we need to do is navigate to https://rustup.rs/. That page will give us a single-step procedure to install the command-line Rust compiler. The procedure differs slightly depending on the platform, but it's never difficult. Here we see the rustup.rs page for Linux:

The installer doesn't just install the Rust compiler, it also installs a tool called rustup that can be used at any time to upgrade our compiler to the latest version. To do this, all we have to do is open up a command-line (or Terminal) window, and type: rustup update.

Upgrading the compiler needs to be simple because the Rust project uses a six-week rapid release schedule, meaning there's a new version of the compiler every six weeks, like clockwork. Each release contains whatever new features have been deemed to be stable in the six weeks since the previous release, in addition to the features of previous releases.

Don't worry, the rapid release of new features doesn't mean that those features were slapped together in the six weeks prior to the release. It's common for them to have spent years in development and testing prior to that. The release schedule just makes sure that, once a feature is deemed to be truly stable, it doesn't take long to get into our hands.

If we aren't willing to wait for a feature to be vetted and stabilized, for whatever reason, we can also use rustup to download, install, and update the beta or nightly releases of the compiler.

To download and install the beta compiler, we just need to type this: rustup toolchain install beta.

From that point on, when we use rustup to update our compiler, it will make sure that we have the newest versions of both the stable and beta compilers. We can then make the beta compiler active with rustup default beta.

Please note that the beta compiler is not the same thing as the next release of the stable compiler. The beta version is where features live before they graduate to stable, and features can and do remain in beta for years.

The nightly version is at most 24 hours behind the development code repository, which means that it might be broken in any number of ways. It's not particularly useful unless you're actually participating in the development of Rust itself. However, should you want to try it out, rustup can install and update it as well. You might also find yourself depending on a library that someone else has written that depends on features that only exist in the nightly build, in which case you'll need to tell rustup that you need the nightly build, too.

One of the things rustup will install is a tool called cargo, which we'll be seeing a lot of in this chapter, and using behind the scenes for the rest of this book. The cargo tool is the frontend to the whole Rust compiler system: it is used for creating new Rust project directories containing the initial boilerplate code for a program or library, for installing and managing dependencies, and for compiling Rust programs, among other things.

Starting a new project

Okay, so we've installed the compiler. Yay! But how do we use it?

The first step is to open up a command-line window, and navigate to the directory where we want to store our new project. Then we can create the skeleton of a new program with cargo new foo.

When we do this, cargo will create a new directory named foo and set up the skeletal program inside it.

The default is for cargo to create the skeleton of an executable program, but we can also tell it to set up a new library for us. All that takes is an additional command-line argument (bar is the name of the new directory that will be created, like foo): cargo new --lib bar.

When we look inside the newly created foo directory, we see a file called Cargo.toml and a sub-directory called src. There may also be a Git version control repository, which we will ignore for now.

Project metadata

The Cargo.toml file is where metadata about the program is stored. That includes the program's name, version number, and authors, but importantly it also has a section for dependencies. Editing the content of the [dependencies] section is how we tell Rust that our code should be linked to external libraries when it is compiled, which libraries and versions to use, and where to find them. External libraries are collections of source code that were packaged up in order to make them easy to use as components of other programs. By finding and linking good libraries, we can save the time and effort of writing our whole program ourselves. Instead, we can write only the part that nobody else has already done.

By the way, .toml files are written in Tom's Obvious, Minimal Language (TOML), a more well-defined and feature-complete version of the old .ini format that Microsoft popularized but never standardized. TOML is becoming quite popular, and is supported and used in a wide variety of languages and applications. You can find the language specification at https://github.com/toml-lang/toml.

Dependencies on libraries from crates.io

If a library that our program depends on is published on https://crates.io/, all we have to do to link it is add its linking code to the dependencies section. Let's say we want to use serde (a tool for turning Rust data into formats such as JSON and back) in our program. First, we find its linking code with: cargo search serde.

I originally found out about serde by browsing through crates.io, an exploration that I would encourage you to try as well.

This will print out a list of matches that looks something like this:


serde = "1.0.70" # A generic serialization/deserialization framework
serde_json = "1.0.24" # A JSON serialization file format
serde_derive_internals = "0.23.1" # AST representation used by Serde derive macros. Unstable.
serde_any = "0.5.0" # Dynamic serialization and deserialization with the format chosen at runtime
serde_yaml = "0.7.5" # YAML support for Serde
serde_bytes = "0.10.4" # Optimized handling of `&[u8]` and `Vec<u8>` for Serde
serde_traitobject = "0.1.0" # Serializable trait objects. This library enables the serialization of trait objects such…
cargo-ssearch = "0.1.2" # cargo-ssearch: cargo search on steroids
serde_codegen_internals = "0.14.2" # AST representation used by Serde codegen. Unstable.
serde_millis = "0.1.1" # A serde wrapper that stores integer millisecond value for timestamps and duration…
... and 458 crates more (use --limit N to see more)

The first one is the core serde library, and the linking code is the part of the line before the # symbol. All we have to do is copy and paste that into the dependencies section of Cargo.toml, and Rust will know that it should compile and link serde when it compiles our foo program. So, the dependencies section of Cargo.toml would look like this:

 [dependencies]
serde = "1.0.70"

Dependencies on Git repositories

Depending on a library stored in the Git version control system, either locally or remotely, is also easy. The linking code is slightly different, but it looks like this:

    [dependencies]
thing = { git = "https://github.com/example/thing" }

We tell Rust where to find the repository, and it knows how to check it out, compile it, and link it with our program. The repository location doesn't have to be a URL; it can be any repository location that the git command recognizes.

Dependencies on local libraries

We can also link against other libraries stored on our own systems, of course. To do this, we just have to add an entry such as this to our Cargo.toml file:


[dependencies]
example = { path = "/path/to/example" }

The path can be absolute or relative. If it's relative, it's interpreted as being relative to the directory containing our Cargo.toml file.

Automatically generated source files

When creating an executable program, cargo adds a file called main.rs to our project as it is created. For a newly created library, it instead adds lib.rs. In either case, that file is the entry point for the whole project.

Let's take a look at the boilerplate main.rs file:

     fn main() {
println!("Hello, world!");
}

Simple enough, right? Cargo's default program is a Rust version of the classic hello world program, which has been re-implemented countless times by new programmers in every conceivable programming language.

If we look at a new library's lib.rs file, things are a little more interesting:

     #[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

Instead of having a main function, which all executable programs need because they need a place to start, the library boilerplate includes a framework for automated tests and a single test that confirms that 2 + 2 = 4.

Compiling our project

The basic command to compile a Rust program is simple: cargo build.

We need to be in the directory containing Cargo.toml (or any subdirectory of that directory) in order to be able to do this, since that's how the cargo program knows which project to compile. However, we don't need to give it any other information, since everything it needs to know is in the metadata file.

Here, we see the result of building the chapter02 source code:

The warnings are expected and do not prevent the compile from succeeding. If we look at those warnings carefully, we can see that Rust is a lot more helpful with its warnings than many programming languages, giving us hints for improving efficiency and such, rather than just talking about language syntax.

When we build the program, a Cargo.lock file and target directory are created.

Cargo.lock records the exact versions of dependencies that were used to build the project, which makes it much easier to produce repeatable results from different compilations of the same program. It's largely safe to ignore this file, as cargo will usually take care of anything that needs to be done with it.

The Rust community recommends that the Cargo.lock file should be added to your version control system (Git, for example) if your project is a program, but not if your project is a library. That's because a program's Cargo.lock file stores all of the versions that resulted in a successful compile of a complete program, where a library's only encompasses part of the picture, and so can lead to more confusion than help when distributed to others.

The target directory contains all of the build artifacts and intermediate files resulting from the compilation process, as well as the final program file. Storing the intermediate files allows future compiles to process only those files that need to be processed, and so speeds up the compilation process.

Our program itself is in the target/debug/foo file (or target\debug\foo.exe on Windows) and we can navigate to it and run it manually if we want to. However, cargo provides a shortcut: cargo run.

We can use that command from any subdirectory of our project, and it will find and run our program for us.

Additionally, cargo run implies cargo build, meaning that if we've changed the source code since the last time we ran the program, cargo run will recompile the program before running it. That means we can just alternate between making changes to our code and executing it with cargo run to see it in action.

Debug and release builds

You may have noticed that the program was in a directory called target/debug. What's that about? By default, cargo builds our program in debug mode, which is what a programmer normally wants.

That means that the resulting program is instrumented to work with the rust-gdb debugging program so we can examine what is happening in its internals, and to provide useful information in crash dumps and such, as well as skipping the compiler's optimization phase. The optimizations are skipped because they rearrange things in such a way that it makes debugging information almost incomprehensible.

However, sometimes a program doesn't have any more bugs (that we know about) and we're ready to ship it out to others. To construct our final, optimized version of the program, we use cargo build --release.

This will construct the release version of the program, and leave it in target/release/foo. We can copy it from there and package it up for distribution.

Dynamic libraries, software distribution, and Rust

For the most part, Rust avoids using dynamic libraries. Instead, all of the dependencies of a Rust program are linked directly into the executable, and only select operating system libraries are dynamically linked. This makes Rust programs a little larger than you might expect, but a few megabytes are of no concern in the era of gigabytes. In exchange, Rust programs are very portable and immune to dynamically linked library version issues.

That means that, if a Rust program works at all, it's going to work on pretty much any computer running roughly the same operating system and architecture it was compiled for, with no hassles. You can take your release version of a Rust program, zip it up, and email it to someone else with confidence that they will have no problem running it.

This doesn't entirely eliminate external dependencies. If your program is a client, the server it connects to needs to be available, for example. However, it does greatly simplify the whole packaging and distribution process.

Using crates.io

We saw cargo search earlier, which allowed us a quick and easy way to find third-party libraries from the command line, so that we could link them with our own program. That's very useful, but sometimes we want a little more information than what that provides. It's really most useful when we know exactly which library we want and just need a quick reference to the linking code.

When we don't know exactly what we want, it's usually better to use a web browser to look around https://crates.io/ and find options.

When we find an interesting or useful library in the web browser, we get the following:

  • The linking code
  • Introductory information
  • Documentation
  • Popularity statistics
  • Version history
  • License information
  • A link to the library's web site
  • A link to the source code

This richer information is useful for figuring out which library or libraries are best suited to our projects. Picking the best libraries for the job saves a lot of time in the end, so the web interface to crates.io is great.

The front page of crates.io shows new and popular libraries, divided up in several ways, and these can be interesting and useful to explore. However, the main value is the search box. Using the search box, we can usually find several candidates for any library needs we may have.

Summary

So, now we know how to install the Rust compiler, set up a Rust project, find and link useful third-party libraries, and compile source code into a usable program. We've also taken a basic look at the boilerplate code that cargo generates when we ask it to set up a new program or library project for us. We've learned about the difference between a debugging build and a release build and taken a quick look at what's involved in distributing a Rust program to users.

Coming up in Chapter 2, Basics of the Rust Language, we're going to begin looking at the Rust programming language itself, rather than the support facilities that surround it. We're going to see how the language is structured and some of the most important commands.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn the semantics of Rust, which can be significantly different from other programming languages
  • Understand clearly how to work with the Rust compiler which strictly enforces rules that may not be obvious
  • Examples and insights beyond the Rust documentation

Description

Rust is an emerging programming language applicable to areas such as embedded programming, network programming, system programming, and web development. This book will take you from the basics of Rust to a point where your code compiles and does what you intend it to do! This book starts with an introduction to Rust and how to get set for programming, including the rustup and cargo tools for managing a Rust installation and development work?ow. Then you'll learn about the fundamentals of structuring a Rust program, such as functions, mutability, data structures, implementing behavior for types, and many more. You will also learn about concepts that Rust handles differently from most other languages. After understanding the Basics of Rust programming, you will learn about the core ideas, such as variable ownership, scope, lifetime, and borrowing. After these key ideas, you will explore making decisions in Rust based on data types by learning about match and if let expressions. After that, you'll work with different data types in Rust, and learn about memory management and smart pointers.

Who is this book for?

This book is for people who are new to Rust, either as their first programming language or coming to it from somewhere else. Familiarity with computer programming in any other language will be helpful in getting the best out of this book.

What you will learn

  • Install Rust and write your first program with it
  • Understand ownership in Rust
  • Handle different data types
  • Make decisions by pattern matching
  • Use smart pointers
  • Use generic types and type specialization
  • Write code that works with many data types
  • Tap into the standard library

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 30, 2018
Length: 180 pages
Edition : 1st
Language : English
ISBN-13 : 9781789610611
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Oct 30, 2018
Length: 180 pages
Edition : 1st
Language : English
ISBN-13 : 9781789610611
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 103.97
Mastering Rust
€41.99
Rust Quick Start Guide
€24.99
Rust Standard Library Cookbook
€36.99
Total 103.97 Stars icon

Table of Contents

9 Chapters
Getting Ready Chevron down icon Chevron up icon
Basics of the Rust Language Chevron down icon Chevron up icon
The Big Ideas – Ownership and Borrowing Chevron down icon Chevron up icon
Making Decisions by Pattern Matching Chevron down icon Chevron up icon
One Data Type Representing Multiple Kinds of Data Chevron down icon Chevron up icon
Heap Memory and Smart Pointers Chevron down icon Chevron up icon
Generic Types Chevron down icon Chevron up icon
Important Standard Traits Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7
(3 Ratings)
5 star 33.3%
4 star 33.3%
3 star 0%
2 star 33.3%
1 star 0%
M. Henri De Feraudy Apr 01, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a lively introduction to Rust. The writing is friendly to beginners, or so it seems to me, because I am not a beginner, so I'm guessing a little. For a more experienced developer, it might be a little too watered down and informal, but I'm not complaining. You have the Rust website as an alternate source of information.You could come to this book after learning Python.
Amazon Verified review Amazon
Amazon Customer Jun 04, 2022
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Concepts are well explained...will recommend The book for those who already know c or cpp.
Amazon Verified review Amazon
Avirup D. Jun 20, 2020
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Very Boring and difficult-to-book. Don't buy it if you are a newcomer in Rust.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.