ZetCode

Introduction to Rust

last modified June 7, 2026

This is an introduction to the Rust programming language. Rust first appeared in 2010 (stable release in 2015) and is developed by the Rust community under the stewardship of the Rust Foundation. The website of the language is https://www.rust-lang.org/.

An online development environment is available at https://play.rust-lang.org/.

Rust Programming Language

Rust is a modern systems programming language focused on safety, speed, and concurrency. It empowers developers to write reliable and efficient software without the overhead of a garbage collector. Its rich type system and ownership model guarantee memory-safety and thread-safety, eliminating many classes of bugs at compile time.

Rust is especially well-suited for performance-critical applications, such as operating systems, game engines, web browsers, and embedded devices. Thanks to its powerful abstractions and zero-cost principles, Rust also excels in web assembly, networking, and command-line tools.

Key features of Rust include:

Rust's ecosystem continues to grow rapidly, with a vibrant community and backing from major companies. It is consistently voted the most admired language in developer surveys.

Rust installation

The recommended way to install Rust is through rustup, the Rust toolchain installer. For current instructions, see the official installation page:

https://www.rust-lang.org/tools/install

On Linux, macOS, and other Unix-like systems, we can install Rust with:

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

After installation, we can verify the installed version with the --version command.

$ rustc --version
rustc 1.91.0 (a5b7c8d3e 2026-06-04)

For VS Code, install the rust-analyzer extension, which provides language support, code completion, inline hints, and debugging capabilities.

Rust simple example

The following is a simple example in Rust.

main.rs
fn main() {
    println!("First program in Rust");
}

The program prints a message to the console. Rust source files use the .rs extension. The main function is the entry point of every Rust program. The function is defined with the fn keyword, and like in Dart, it does not return a value by default.

The body of the function is enclosed in curly brackets. The println! macro prints a string to the standard output, followed by a newline. The exclamation mark indicates that it is a macro, not a regular function. Statements are terminated with a semicolon.

$ rustc main.rs
$ ./main
First program in Rust

Rust variables

Variables store values, and by default they are immutable.

main.rs
fn main() {
    let name = "John Doe";
    let age = 34;

    println!("{} is {} years old", name, age);
}

In the example, we declare a string and an integer variable. The let keyword creates a variable, and Rust infers the types automatically (in this case &str for the string literal and i32 for the integer).

let name = "John Doe";

Variable bindings are immutable by default. To make them mutable, we use let mut. Here we don't need mutability because we never reassign the variables.

println!("{} is {} years old", name, age);

Rust uses placeholders like {} inside the format string to insert values. The variables are provided as subsequent arguments.

$ rustc main.rs && ./main
John Doe is 34 years old

Rust user input

The std::io module provides input/output operations for the console.

main.rs
use std::io::{self, Write};

fn main() {
    print!("Enter your name: ");
    io::stdout().flush().unwrap();

    let mut name = String::new();
    io::stdin()
        .read_line(&mut name)
        .expect("Failed to read line");

    let name = name.trim();
    println!("Hello {}", name);
}

The example prompts the user for a name and prints a greeting.

use std::io::{self, Write};

We import the std::io module and the Write trait. The self keyword is used to import the module itself.

print!("Enter your name: ");
io::stdout().flush().unwrap();

We use the print! macro to write a prompt without a newline. Because standard output is line-buffered, we manually flush it with io::stdout().flush() and call unwrap() to handle potential errors (panicking if something goes wrong). This ensures the prompt is displayed before waiting for input.

The unwrap() method is a common way to handle errors in Rust, and it will cause the program to panic if an error occurs, printing the error message. However, in production code, you would typically want to handle errors more gracefully using expect() or other error-handling techniques.

let mut name = String::new();
io::stdin()
    .read_line(&mut name)
    .expect("Failed to read line");

We create a mutable String to hold the input. The read_line method appends the user's input (including a trailing newline) to the string. The expect method handles any error by printing the given message and terminating the program.

let name = name.trim();

We remove the trailing newline by calling trim(). This returns a string slice (&str) without the whitespace.

$ rustc main.rs && ./main
Enter your name: Peter
Hello Peter

Source

The Rust Programming Language (The Book)
Rust by Example

This was an introduction to the Rust programming language.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Rust tutorials.