Rust Programming for Absolute Beginners
Introduction to Rust
What is Rust?
Rust is a modern programming language focused on three things: safety, speed, and concurrency. Think of it like building with high-tech Lego bricks that only fit together in ways that are strong and stable. This design helps you avoid common bugs that plague other languages, all while running your code incredibly fast.
Rust is a systems programming language that focuses on safety, concurrency, and performance.
Developed at Mozilla, Rust is designed for performance-critical services, running on embedded devices, and easily integrating with other languages. Its compiler is famously helpful, often pointing out potential errors and suggesting fixes before you even run your program.
Setting Up Your Workspace
Getting started with Rust is straightforward. The primary tool you'll use is rustup, which installs the Rust compiler, the standard library, and a handy tool called Cargo.
Cargo is Rust's build tool and package manager. It handles compiling your code, downloading the libraries your project depends on, and building those libraries.
To install rustup on macOS, Linux, or other Unix-like systems, open your terminal and run this command:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
If you're on Windows, visit the official Rust website for the rustup-init.exe installer. Once installed, you'll have access to the rustc compiler and cargo from your command line.
Your First Rust Program
Let's create a classic "Hello, World!" program. Cargo makes this easy. Navigate to the directory where you want to keep your projects and run:
cargo new hello_world
Cargo generates a new directory called hello_world with a couple of files inside. The most important one is src/main.rs, which contains your application's source code. It should already contain this:
fn main() {
println!("Hello, world!");
}
Let's break this down.
fn main() { ... } defines the main function. This is the entry point for every executable Rust program. All the code inside the curly braces {} will be executed when the program runs.
println!("Hello, world!"); prints text to the screen. The ! indicates that println! is a macro, not a regular function. For now, just know that macros are a way of writing code that writes other code in Rust.
To run this program, navigate into the new directory and use Cargo:
cd hello_world
cargo run
Cargo will compile your project and run the resulting executable. You should see Hello, world! printed to your terminal.
Variables and Data Types
In Rust, you declare a variable using the let keyword. By default, variables are immutable, meaning once a value is assigned, it cannot be changed.
fn main() {
let x = 5;
println!("The value of x is: {}", x);
// This line would cause an error!
// x = 6;
}
This focus on immutability helps you write code that is safer and easier to reason about.
If you need a variable that can be changed, you can make it mutable by adding the mut keyword.
fn main() {
let mut y = 10;
println!("The initial value of y is: {}", y);
y = 20;
println!("The new value of y is: {}", y);
}
Rust is a statically typed language, which means it must know the types of all variables at compile time. However, the compiler can usually infer the type based on the value you assign.
Here are a few fundamental data types:
- Integers: Whole numbers. They can be signed (like
i32, which can be negative) or unsigned (likeu32, which can only be positive). - Floating-Point Numbers: Numbers with a decimal point, like
f64. - Booleans: The
booltype, which can only betrueorfalse. - Characters: The
chartype, for single Unicode characters, specified with single quotes.
// We can explicitly declare the type
let logical: bool = true;
// Or let Rust infer the type
let a_float = 1.0; // f64
let an_integer = 5; // i32
let a_character = 'a'; // char
Basic Input and Output
You've already seen how to output text with println!. To get input from the user, you can use Rust's standard library. Here's a quick example of a program that asks for your name and then greets you.
use std::io;
fn main() {
println!("Please enter your name:");
let mut name = String::new();
io::stdin()
.read_line(&mut name)
.expect("Failed to read line");
println!("Hello, {}!", name.trim());
}
This code introduces a few new concepts. It brings the io (input/output) library into scope, creates a new, empty, mutable String to hold the user's input, and then reads a line from the terminal into that string. The .trim() method removes any extra whitespace from the input.
You now have the basic building blocks to start writing simple Rust programs.
What are the three core goals that guide the design of the Rust programming language?
Which command is used to create a new Rust project named my_app using Cargo?
