"The C Programming Language" starts off like this:

The only way to learn a new programming language is by writing programs in it. The first program to write is the same for all languages: Print the words hello, world. This is a big hurdle; to leap over it you have to be able to create the program text somewhere, compile it successfully, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy

We will follow in this tradition and create our first program, one that prints "Hello, world!".

Your Rust installation comes with a tool, Cargo, that manages everything about your projects. It invokes rustc the Rust compiler, to build your project. If you depend on external libraries, it will download them for you, compile them for you, and let your program use them. It will also help you generate new projects! In your terminal, cd into a directory where you want to practice programs. I use a "tmp" folder in my home directory for this.

❯ cargo new hello
     Created binary (application) `hello` package

cargo new will create a new application with the name you pass to it. Let's take a look:

❯ cd hello
❯ cat src\\main.rs
fn main() {
    println!("Hello, world!");
}

Note that we're looking at the [main.rs](<http://main.rs>) file inside a src directory. This is the standard entry point for Rust programs. I use Windows, so I use a \\ for the directory separator; if you're on a Mac or Linux, you'll use /.

Let's try running it. We can ask Cargo to do so:

❯ cargo run
   Compiling hello v0.1.0 (C:\\Users\\steve\\tmp\\hello)
    Finished dev [unoptimized + debuginfo] target(s) in 1.56s
     Running `target\\debug\\hello.exe`
Hello, world!

If you see output similar to this, congrats! You've successfully compiled and run your first Rust program, that makes you a Rustacean! Welcome!

Let's back up and talk about this program. Rust programs begin with a function named main. Functions are defined with the fn keyword, and then use curly braces for the body. The body is indented with four spaces. The main function takes no arguments, and so there's an empty argument list: parenthesis with nothing inside them. println! is used to print a string to your terminal with a newline after it. That's what the ln bit is for; you can also print! and get no newline. The ! means it's a macro, which we'll talk about later. Strings use double quotes, and most lines end with a semicolon.

Exercises

  1. Use the print! macro instead of println! and see how the output changes without the newline.
  2. Change the message to say something other than "Hello, world!"

A slightly more interesting program

Let's build a program that does a bit more than printing a message to the screen. We're going to build a variant of a classic command line tool called wc. This is short for "word count." Change to your project directory, and let's make a new project:

❯ cd ~\\tmp
❯ cargo new wc
     Created binary (application) `wc` package
❯ cd wc

Let's open up src/main.rs in an editor, and write this program:

use std::io::Read;

fn main() {
    dbg!(std::io::stdin().bytes().count());
}