Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Rust Essentials

You're reading from   Rust Essentials A quick guide to writing fast, safe, and concurrent systems and applications

Arrow left icon
Product type Paperback
Published in Nov 2017
Publisher
ISBN-13 9781788390019
Length 264 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Ivo Balbaert Ivo Balbaert
Author Profile Icon Ivo Balbaert
Ivo Balbaert
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Starting with Rust FREE CHAPTER 2. Using Variables and Types 3. Using Functions and Control Structures 4. Structuring Data and Matching Patterns 5. Higher Order Functions and Error-Handling 6. Using Traits and OOP in Rust 7. Ensuring Memory Safety and Pointers 8. Organizing Code and Macros 9. Concurrency - Coding for Multicore Execution 10. Programming at the Boundaries 11. Exploring the Standard Library 12. The Ecosystem of Crates

Our first program

Let's get started by showing a welcome message to the players of our game. Open your favorite text editor (like Notepad++ or gedit) for a new file and type in the following code:

// code in Chapter1\code\welcome.rs 
fn main() { 
    println!("Welcome to the Game!"); 
} 

The steps to be performed are as follows:

  1. Save the file as welcome.rs. The .rs extension is the standard extension of Rust code files. Source file names may not contain spaces; if they contain more than one word, you can use an underscore, _, as a separator, for example: start_game.rs.
  2. Then compile it to native code on the command line with rustc.welcome.rs. This produces an executable program, welcome.exe, on Windows or welcome on Linux.
  3. Run this program with welcome or./welcome to get the output:
Welcome to the Game!  

The output executable gets its name from the source file. If you want to give the executable another name, like start, compile it with the option -o output_name, as shown below:

rustc welcome.rs -o start

The rustc -O produces native code optimized for execution speed (equivalent to rustc -C opt-level=2); the most optimized code is generated for rustc -C opt-level=3.

Compiling and running are separate consecutive steps, contrary to dynamic languages like Ruby or Python where these are performed in one step.

Let's explain the code a bit. If you have already worked in a C, or Java, or C# like environment, this code will seem quite familiar. As in most languages, execution of code starts in a main() function, which is mandatory in an executable program.

In a larger project with many source files, the file containing the main() function would be called main.rs by convention.

We see that main() is a function declaration because it is preceded by the keyword fn, short and elegant like most Rust keywords. The () after main denotes the parameter list, which is empty here. The function's code is placed in a code block, surrounded by curly braces { }, where the opening brace is put by convention on the same line as the function declaration, but separated by one space. The closing brace appears after the code, in the column right beneath fn.

Our program has only one line, which is indented by four spaces to improve readability (Rust is not whitespace sensitive). This line prints the string Welcome to the Game!. Rust recognizes this as a string, because it is surrounded by double quotes " ". This string was given as argument to the println! macro (the ! indicates it is a macro and not a function). The code line ends in a semicolon, ;, as most, but not all, code lines in Rust do (see Chapter 2, Using Variables and Types).

Exercises:
Write, compile, and execute a Rust program, name.rs, that prints out your name.
What is the smallest possible program in Rust in terms of code size?

The println! macro has some nice formatting capabilities and at the same time checks when compiling whether the type of variables is correct for the applied formatting (see Chapter 2, Using Variables and Types).

Working with Cargo

Cargo is Rust's package and dependency manager, like Bundler, npm, pub, or pip for other languages. Although you can write Rust programs without it, Cargo is nearly indispensable for any larger project. It works the same whether you work on a Windows, Linux, or OS X system. The installation procedure from the previous section includes the Cargo tool, cargo, so Rust is shipped with the batteries included.

Cargo does the following things for you:

  • It makes a tidy folder structure and some templates for your project, with the following command:
    cargo new  
  • It compiles (builds) your code, using the following command:
    cargo build
  • It runs your project, using the following command:
    cargo run
  • If your project contains unit-tests, it can execute them for you, using the following command:
    cargo test
  • If your project depends on packages, it will download them and it will build these packages according to the needs of your code, using the following command:
    cargo update

We'll introduce how to use Cargo now, and we'll come back to it later, but you can find more info at http://doc.crates.io/guide.html.

Let's remake our first project, welcome, using Cargo through the following steps:

  1. Start a new project, welcomec, with the following command:
cargo new welcomec --bin  
  • The option --bin tells Cargo that we want to make an executable program (a binary). This outputs the message Created binary (application) `welcomec` project and creates the following directory structure:
  • A folder with the same name as the project is created as a local Git project. In this folder, you can put all kinds of general info such as a License file, a README file, and so on. Also, a subfolder, src, is created, containing a template source file named main.rs (this contains the same code as our welcome.rs, but prints out the string "Hello, world!").
  • The file Cargo.toml (with a capital C) is the configuration file or manifest of your project; it contains all the metadata Cargo needs to compile your project. It follows the so called TOML format (for more details about this format, see https://github.com/toml-lang/toml), and contains the following text with information about the project:
[package] 
name = "welcomec" 
version = "0.1.0" 
authors = ["Your name <you@example.com>"] 
[dependencies]
  • This file is editable and other sections can be added. For example, you can add a section to tell Cargo that we want a binary with name:
welcome: 
[[bin]]   
name = "welcome" 
  1. We build our project (no matter how many source files it contains) with the following command:
cargo build
  • Which gives us the following output (on Linux):
    Compiling welcomec v0.1.0 (file:///home/ivo/Rust_Book/welcomec)
    Finished dev [unoptimized + debuginfo] target(s) in 0.66 secs
  • Now, the following folder structure is produced:
  • The target/debugdirectory contains the executable welcome.
  1. To execute this program, give the following command:
    cargo run
  • Which produces as output:
    Running `target/debug/welcome`
    Hello, world!

Step 2 has also produced a file called Cargo.lock; this is used by Cargo to keep track of dependencies in your application. At this moment, it contains only the following:

[root] 
name = "welcomec" 
version = "0.1.0" 

The same format is used to lock down the versions of libraries or packages your project depends on. If your project is built in the future, when updated versions of the libraries are available, Cargo will make sure that only the versions recorded in Cargo.lock are used, so that your project is not built with an incompatible version of a library. This ensures a repeatable build process.

The cargo -list gives you an overview of the commands you can use within this tool.

Exercise:
Make, build, and run a project, name, that prints out your name with Cargo.

The site https://crates.io/ is the central repository for Rust packages, or crates as they are called, containing over 10000 crates at the end of June 2017. You can search for crates with specific terms, or browse them alphabetically or by number of downloads. The site looks like the following:

You have been reading a chapter from
Rust Essentials - Second Edition
Published in: Nov 2017
Publisher:
ISBN-13: 9781788390019
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime