Transcription of The Rust Programming Language - GitHub Pages
1 The rust Programming Language The rust Programming Language The rust Project Developpers The rust Programming Language , The rust Project Developpers. Contents I Introduction 7. 1 The rust Programming Language 9. II Getting Started 11. 1 Getting Started 13. 2 Installing rust 15. 3 Hello, world! 17. 4 Hello, Cargo! 21. 5 Closing Thoughts 27. III Tutorial: Guessing Games 29. 1 Guessing Game 31. 2 Set up 33. 3 Processing a Guess 35. 4 Generating a secret number 41. 5 Comparing guesses 47. 6 Looping 53. 7 Complete! 61. 6. IV Syntax and Semantics 63.
2 1 Syntax and Semantics 65. V Effective rust 245. 1 Effective rust 247. VI Appendix 393. 1 Glossary 395. 2 Syntax Index 399. 3 Bibliography 409. Part I. Introduction Chapter 1. The rust Programming Language Welcome! This book will teach you about the rust Programming Lan- guage. rust is a systems Programming Language focused on three goals: safety, speed, and concurrency. It maintains these goals without hav- ing a garbage collector, making it a useful Language for a number of use cases other languages aren't good at: embedding in other languages, programs with specific space and time requirements, and writing low- level code, like device drivers and operating systems.
3 It improves on current languages targeting this space by having a number of compile- time safety checks that produce no runtime overhead, while eliminat- ing all data races. rust also aims to achieve zero-cost abstractions'. even though some of these abstractions feel like those of a high-level Language . Even then, rust still allows precise control like a low-level Language would. The rust Programming Language is split into chapters. This introduction is the first. After this: Getting started - Set up your computer for rust development. Tutorial: Guessing Game - Learn some rust with a small project.
4 Syntax and Semantics - Each bit of rust , broken down into small chunks. Effective rust - Higher-level concepts for writing excellent rust code. 10. Glossary - A reference of terms used in the book. Bibliography - Background on rust 's influences, papers about rust . Contributing The source files from which this book is generated can be found on GitHub . Second edition of this book There are two editions of The rust Programming Language , this being the first edition. The second edition is a complete re-write. It is still under construc- tion, though it is far enough along to learn most of rust .
5 We suggest reading the second edition and then checking out the first edition later to pick up some of the more esoteric parts of the Language . Part II. Getting Started Chapter 1. Getting Started This first chapter of the book will get us going with rust and its tool- ing. First, we'll install rust . Then, the classic Hello World' program. Finally, we'll talk about Cargo, rust 's build system and package man- ager. We'll be showing off a number of commands using a terminal, and those lines all start with $. You don't need to type in the $s, they are there to indicate the start of each command.
6 We'll see many tuto- rials and examples around the web that follow this convention: $ for commands run as our regular user, and # for commands we should be running as an administrator. 14. Chapter 2. Installing rust The first step to using rust is to install it. Generally speaking, you'll need an Internet connection to run the commands in this section, as we'll be downloading rust from the Internet. The rust compiler runs on, and compiles to, a great number of platforms, but is best supported on Linux, Mac, and Windows, on the x86 and x86-64 CPU architecture.
7 There are official builds of the rust compiler and standard library for these platforms and more. For full details on rust platform support see the website. Installing rust All you need to do on Unix systems like Linux and macOS is open a terminal and type this: $ curl -sSf | sh It will download a script, and start the installation. If everything goes well, you'll see this appear: rust is installed now. Great! Installing on Windows is nearly as easy: download and run rustup- It will start the installation in a console and present the above message on success.
8 For other installation options and information, visit the install page of the rust website. 16. Uninstalling Uninstalling rust is as easy as installing it: $ rustup self uninstall Troubleshooting If we've got rust installed, we can open up a shell, and type this: $ rustc --version You should see the version number, commit hash, and commit date. If you do, rust has been installed successfully! Congrats! If you don't, that probably means that the PATH environment vari- able doesn't include Cargo's binary directory, ~/.cargo/bin on Unix, or %USERPROFILE%\.
9 Cargo\bin on Windows. This is the directory where rust development tools live, and most rust developers keep it in their PATH environment variable, which makes it possible to run rustc on the command line. Due to differences in operating systems, com- mand shells, and bugs in installation, you may need to restart your shell, log out of the system, or configure PATH manually as appropriate for your operating environment. rust does not do its own linking, and so you'll need to have a linker installed. Doing so will depend on your specific system.
10 For Linux- based systems, rust will attempt to call cc for linking. On windows- msvc ( rust built on Windows with Microsoft Visual Studio), this de- pends on having Microsoft Visual C++ Build Tools installed. These do not need to be in %PATH% as rustc will find them automatically. In general, if you have your linker in a non-traditional location you can call rustc linker=/path/to/cc, where /path/to/cc should point to your linker path. If you are still stuck, there are a number of places where we can get help. The easiest is the # rust -beginners IRC channel on and for general discussion the # rust IRC channel on , which we can access through Mibbit.