2 Language foundations

 

This chapter covers

  • Coming to grips with the Rust syntax
  • Learning fundamental types and data structures
  • Building command-line utilities
  • Compiling programs

This chapter introduces you to the fundamentals of Rust programming. By the end of the chapter, you will be able to create command-line utilities and should be able to get the gist of most Rust programs. We’ll work through most of the language’s syntax, but defer much of the detail about why things are how they are for later in the book.

Note

Programmers who have experience with another programming language will benefit the most from this chapter. If you are an experienced Rust programmer, feel free to skim through it.

Beginners are welcomed. Rust’s community strives to be responsive to newcomers. At times, you may strike a mental pothole when you encounter terms such as lifetime elision, hygienic macros, move semantics, and algebraic data types without context. Don’t be afraid to ask for help. The community is much more welcoming than these helpful, yet opaque, terms might suggest.

In this chapter, we will build grep-lite, a greatly stripped-down version of the ubiquitous grep utility. Our grep-lite program looks for patterns within text and prints lines that match. This simple program allows us to focus on the unique features of Rust.

2.1 Creating a running program

2.1.1 Compiling single files with rustc

2.1.2 Compiling Rust projects with cargo

2.2 A glance at Rust’s syntax

2.2.1 Defining variables and calling functions

2.3 Numbers

2.3.1 Integers and decimal (floating-point) numbers

2.3.2 Integers with base 2, base 8, and base 16 notation

2.3.3 Comparing numbers

2.3.4 Rational, complex numbers, and other numeric types

2.4 Flow control

2.4.1 For: The central pillar of iteration

2.4.2 Continue: Skipping the rest of the current iteration

sitemap