Chapter 1. Truth or Consequences
And the truth is, we don’t know anything
They Might Be Giants, “Ana Ng” (1988)
In this chapter, I’ll show you how to organize, run, and test a Rust program. I’ll be using a Unix platform (macOS) to explain some basic ideas about command-line programs. Only some of these ideas apply to the Windows operating system, but the Rust programs themselves will work the same no matter which platform you use.
You will learn how to do the following:
-
Compile Rust code into an executable
-
Use Cargo to start a new project
-
Use the
$PATHenvironment variable -
Include external Rust crates from crates.io
-
Interpret the exit status of a program
-
Use common system commands and options
-
Write Rust versions of the
trueandfalseprograms -
Organize, write, and run tests
Getting Started with “Hello, world!”
It seems the universally agreed-upon way to start learning a programming language is printing “Hello, world!” to the screen.
Change to a temporary directory with cd /tmp to write this first program.
We’re just messing around, so we don’t need a real
directory yet.
Then fire up a text editor and type the following code into a file called hello.rs:
fnmain(){println!("Hello, world!");}
Functions are defined using
fn. The name of this function is ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access