2 Language Foundations

 

In this chapter:

  • Getting to grips with Rust syntax
  • Learning the 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 be working through most of the language’s syntax, but deferring much of the detail about why things are how they are for later in the book.

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.

We will be building grep-lite, a much stripped down version of the ubiquitous grep utility. grep-lite looks for patterns within text, printing lines that match. A simple program allows us to focus on the unique features of Rust.

Tip

Beginners Are Welcome

Rust’s community strives to be welcome and 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 may suggest.

2.1 Create a running program

2.1.1 Compiling single files

2.1.2 Compiling larger projects

2.2 A glance at Rust’s syntax

2.2.1 Defining 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 Iteration

2.4.1 Creating iterators that support for loops

2.5 Flow control

2.5.1 for: the central pillar of iteration

2.5.2 continue: Skipping the rest of the current iteration

2.5.3 while: Looping until a condition changes its state

2.5.4 loop: The basis for Rust’s looping constructs

2.5.5 break: Aborting a loop

sitemap