HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RustVariablesData TypesOperatorsStringsVectors (Vec)Conditions (if / match)Loops (for / while)FunctionsOwnership & Borrowing
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Vector & HashMap
🔁 Condition & Loop
🧩 Function
🔑 Ownership & Borrowing
Rust›Introduction to Rust

Introduction to Rust

First step into Rust, the safe systems language: fn main structure, println! macro, and variables.

Simple enough for a 5-year-old to understand

fn main and println!

Every Rust program starts with fn main(). To print to the screen, the println! macro is used (! is mandatory).

Your first program
1
fn main() {
Program entry point.
FunctionProgram entry point.
2
println!("Hello!");
Prints and moves to the next line.
Output / InputPrints and moves to the next line.
3
}
End of main.
FunctionEnd of main.
FunctionOutput / Input
💡 Hint: println! is a macro; don't forget the ! at the end.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (2 left)

1.What is used to print to the screen in Rust?
2.Print exactly Hello World to the screen.

🎯 Expected output: Hello World

Loading editor…

About this topic

Rust was developed at Mozilla in the 2010s to fundamentally solve memory bugs in browser engines. The goal was clear: to be as fast as C and C++, but to make memory bugs impossible at compile time.

It achieves this without a garbage collector. The Rust compiler tracks who owns every value and when it will be destroyed while the code is being written. Error classes like dangling pointers, double frees, and data races do not even compile.

The price for this is the learning curve. The Rust compiler rejects errors at compile time that other languages report by crashing at runtime, so you get a lot of error messages at first. However, the error messages are unusually descriptive and usually tell you the solution.

What you will learn

  • the fn main structure and program flow
  • why the println macro has an exclamation mark
  • how to create and run a project with cargo
  • how to read compiler error messages

Who is this for?

  • Those who want to do system programming but do not want to deal with memory bugs
  • Those who know C or C++ and are looking for a safer option
  • Those who will write command line tools and backend services

When to use?

Rust is used where performance and reliability are required together: network services, command-line tools, game engines, operating system components, browser engines.

It is also strong in the WebAssembly target; it is a common choice for performance-critical code running in the browser.

Common mistakes

Wrong

fn main() {
    println("Hello");
}

println is a macro, not a function, and is called with an exclamation mark. The compiler gives the error "cannot find function println in this scope" and usually prints a help line suggesting you add the exclamation mark. The reason it is a macro is that it takes a variable number of arguments.

Correct

fn main() {
    println!("Hello");
}

Wrong

fn main() {
    let name = "Ali";
    println!("Hello " + name);
}

In Rust, string concatenation is not done this way with a plus sign, and the format string must be a constant at compile time. The compiler gives the error "format argument must be a string literal". Values are inserted using curly brace placeholders.

Correct

fn main() {
    let name = "Ali";
    println!("Hello {}", name);
}

Wrong

fn main() {
    let total = 5
    println!("{}", total);
}

There is no semicolon at the end of the statement. Rust tries to combine this with the next line and gives the error "expected ';'" . In Rust, the semicolon is not just a separator: it determines the distinction between a statement and an expression, and carries meaning in function returns.

Correct

fn main() {
    let total = 5;
    println!("{}", total);
}

Frequently asked questions

What is the difference between a macro and a function?

A macro expands into code at compile time, while a function is called at runtime. println is a macro because it takes a variable number of arguments and checks the format string at compile time — if the number of placeholders does not match the number of arguments, the code will not compile.

What is cargo, is it a compiler?

No, it is a package and build manager; it calls the compiler. It creates projects, downloads dependencies, compiles, runs, and executes tests. In the Rust ecosystem, calling the compiler directly is rare except for single-file experiments.

Is learning Rust really hard?

The first few weeks are challenging because the compiler rejects code that other languages accept. However, the things it rejects are real bugs; in other languages, those same bugs appear at runtime. The challenge is that debugging is shifted forward.

Summary

Rust ensures memory safety at compile time without a garbage collector.

println is a macro and is called with an exclamation mark.

Values are formatted with curly brace placeholders.

Compiler error messages are descriptive and usually suggest a solution.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code