Carlo Milanesi
Beginning Rust
Get Started with Rust 2021 Edition
2nd ed.
Logo of the publisher
Carlo Milanesi
Bergamo, Italy
ISBN 978-1-4842-7207-7 e-ISBN 978-1-4842-7208-4
https://doi.org/10.1007/978-1-4842-7208-4
Carlo Milanesi 2022
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
This Apress imprint is published by the registered company APress Media, LLC part of Springer Nature.
The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.
This book is dedicated to my mother and to my fathers memory. I owe them my interest in learning and explaining things.
Preface
Welcome to Beginning Rust. This book teaches beginners how to program with the exciting Rust programming language in an easy, step-by-step manner. It is a fully revised version of the 2018 book, with several additions, expanded clarifications for the most hard-to-master concepts, and removal of redundant sections. It utilizes the 2021 edition of the Rust language, developed by the Rust Team community.
What You Need to Know
Only a basic knowledge of programming is required. Rust is mainly targeted for systems programming tasks, a sector currently dominated by the C and C++ languages. So, to ease the transition from such languages, the text contains many comparisons with C and C++, and even code examples in those languages. Yet, this book can also be enjoyed by people who are only familiar with other programming languages, like Java or C#.
Knowledge of command-line environments is assumed, like a Linux shell, macOS Terminal, or Windows Command Prompt, because all the examples contained in this book need to be compiled and run in such environments.
How to Read This Book
It is strongly recommended you read the book from beginning to end, without skipping chapters, because every chapter assumes that the reader understands what was explained in previous chapters.
The text presents a lot of code examples, which should be read thoroughly, and possibly compiled and run on your own computer. You can type them, or use the freely available downloaded code.
Where to Find the Source Code
All of the examples used in this book can be accessed on GitHub at github.com/apress/beginning-rust-2e .
What This Book Does Not Cover
Please note that this book does not cover everything needed to develop professional programs using Rust. It just teaches the concepts of the language, including the most difficult ones. For writing professional software, you will need to download and learn some third-party frameworks and libraries. There are quite a few of them which are of good-to-outstanding quality and freely available.
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub ( github.com/apress/beginning-rust-2e ). For more detailed information, please visit www.apress.com/source-code .
Table of Contents
About the Author
Carlo Milanesi
is a professional software developer, expert in C++, graphics programming, and GUI design. He graduated from the State University of Milan and has worked in the financial and CAD/CAM software industries. He enjoys designing, writing, and testing software. He also loves teaching and acting.
He authored the book Creative Projects for Rust Programmers, and developed the Rust library https://github.com/carlomilanesi/rs-measures .
About the Technical Reviewer
Satej Kumar Sahu
works in the role of Senior Enterprise Architect at Honeywell. He is passionate about technology, people, and nature. He believes that through technology and conscientious decision making, each of us has the power to make this world a better place. In his free time, he can be found reading books, playing basketball, and having fun with friends and family.
The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
C. Milanesi Beginning Rust https://doi.org/10.1007/978-1-4842-7208-4_1
1. Getting Started
In this chapter, you will learn:
How to write and run your first program in the Rust language
How to print text and numbers on the terminal
How to write comments in your code
How to Start
The smallest valid Rust program is this:
fn main(){}
Of course, it does nothing. It just defines an empty function named main. By function we mean a set of instructions that does something, and that has been given a name.
fn is shorthand for function, while main is the name of this function. The round parentheses contain the functions possible arguments; in this case, there are no arguments. To close, the braces contain the possible statements that comprise the body of the function; in this case there are no statements.
When a program written in Rust is run, its main function is executed. If there is no main function, then it isnt a complete program; it may be a library, though. Any Rust library may contain several entry points; instead, any Rust program has just one entry point, and its name must be main .
To run this program, first you must install the Rust toolset . The official Rust toolset can be downloaded for free from the website www.rust-lang.org . Linux, Windows, and macOS platforms are supported. For each platform, there are three versions: stable, beta, and nightly. The stable version is recommended; it is the oldest, but also the most tested one and the one less likely to change. All of these program versions should be used from the command line of a console. After installation, to check which version is installed, type at a command line (with uppercase V ): rustc -V . The code in this book has been checked using version 1.56.0, but probably later versions will be OK too.