Rust Intro
Rust is a programming language for building fast, reliable software. The compiler checks the code before the program runs.
Every Rust program starts in main
fn main() is where the program begins. Inside it, println! prints a line of text:
fn main() {
println!("Hello, Rust!");
}A slot waits for a value
let makes a named value. The {} in the text is an empty slot. Rust fills it with the value you name after the text:
let name = "Ferris";
println!("Hello, {}!", name);The ! means macro
println! ends with ! because it is a macro β a small piece of code that writes code for you:
println!("This line was printed by a macro.");Cargo builds and runs it
Cargo is the built-in tool that builds your project and manages extra libraries. One command builds a project and runs it:
# in your terminal:
# $ cargo runExample
fn main() {
let name = "Ferris";
println!("Hello, {}! Welcome to Rust.", name);
}Key points
- Safety first: the compiler checks your code before the program runs
- No garbage collector: a garbage collector is a background helper in some languages that hunts for memory nobody needs anymore.
- Rust has none β it cleans up memory by rule instead
- Fearless concurrency: using many threads at once is checked for mistakes, too.
- A thread is one strand of work inside a program
- Zero-cost abstractions: clear, high-level code still runs very fast
- How this course runs: walk the sections in order.
- Each lesson builds on the ones before it.
- Each lesson ends with a small exercise.
- Five Capstone projects at the very end turn reading into building.
- In short, Rust is a fast language that checks code before the program runs.
- Rust builds tools, servers, browsers, and programs for tiny devices.
In this example, we save the name "Ferris" in a variable called name. Then println! prints a line of text. The {} is an empty slot in the text. Rust fills the slot with the value of name. The ! shows that println! is a macro, a small piece of code that writes code for you. Every Rust program starts inside fn main().
Note
Rust has no null, the "nothing here" value many languages use.
It has no hidden memory cleanup either.
The rules you will learn replace both.
Try it Yourself
Edit the code, then press Run.
Running is simulated Β· Rust Playground
Exercise
What does the Rust compiler promise to check before your program runs?