Book Image

Rust Essentials

By : Ivo Balbaert
Book Image

Rust Essentials

By: Ivo Balbaert

Overview of this book

<p>Starting by comparing Rust with other programming languages, this book will show you where and how to use Rust. It will discuss primitive types along with variables and their scope, binding and casting, simple functions, and ways to control execution flow in a program.</p> <p>Next, the book covers flexible arrays, vectors, tuples, enums, and structs. You will then generalize the code with higher-order functions and generics applying it to closures, iterators, consumers, and so on. Memory safety is ensured by the compiler by using references, pointers, boxes, reference counting, and atomic reference counting. You will learn how to build macros and crates and discover concurrency for multicore execution.</p> <p>By the end of this book, you will have successfully migrated to using Rust and will be able to use it as your main programming language.</p>
Table of Contents (17 chapters)
Rust Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Expressions


Rust is an expression-oriented language, which means that most pieces of code are in fact expressions, that is, they compute a value and return that value (in that sense, values are also expressions). However, expressions by themselves do not form meaningful code; they must be used in statements.

The let bindings like the following are declaration statements; they are not expressions:

// see Chapter 2/code/expressions.rs
let a = 2;    // a binds to 2
let b = 5;    // b binds to 5
let n = a + b;   // n binds to 7

However, a + b is an expression, and if we omit the semicolon at the end, the resulting value (here 7) is returned. This is often used when a function needs to return its value (see examples in the next chapter). Ending an expression with a semicolon like a + b; suppresses the value of an expression, thereby throwing away the return value and making it an expression statement that returns the unit value (). A code is usually a sequence of statements, one on each code line...