Book Image

Learning Rust

By : Vesa Kaihlavirta
Book Image

Learning Rust

By: Vesa Kaihlavirta

Overview of this book

Rust is a highly concurrent and high performance language that focuses on safety and speed, memory management, and writing clean code. It also guarantees thread safety, and its aim is to improve the performance of existing applications. Its potential is shown by the fact that it has been backed by Mozilla to solve the critical problem of concurrency. Learning Rust will teach you to build concurrent, fast, and robust applications. From learning the basic syntax to writing complex functions, this book will is your one stop guide to get up to speed with the fundamentals of Rust programming. We will cover the essentials of the language, including variables, procedures, output, compiling, installing, and memory handling. You will learn how to write object-oriented code, work with generics, conduct pattern matching, and build macros. You will get to know how to communicate with users and other services, as well as getting to grips with generics, scoping, and more advanced conditions. You will also discover how to extend the compilation unit in Rust. By the end of this book, you will be able to create a complex application in Rust to move forward with.
Table of Contents (21 chapters)
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Title Page
Preface
Free Chapter
1
Introducing and Installing Rust
4
Conditions, Recursion, and Loops

Something you may have noticed


If we compare the two different implementations of the code (generic and non-generic), the main difference is that we have reduced the amount of code we need as the two structs were the same in all but the name. We have also simplified the code so that we have a single call to calc and allow the compiler to decide which one we need based on the type passed in.

Generics - a small aside

Code reduction and simplification is always a good thing (well, mostly at least!). However, with generics, there is always a trade-off and it's not always apparent.

Let's consider the following piece of code:

fn my_multiply<T: Mul<Output = T>>(a: T, b: T) -> T { return a * b; }

This returns a value of type T by multiplying two variables (of type T).

The question is: You can send a number of types into that function - how will the compiler know what to do if it doesn't know what type T is? The only safe way is to create a version of my_multiply for each possible type....