Fondamenti di Rust

Hello World in Rust: Structure of a Program

Write your first Rust program and see what each part does: fn main, the braces and println!.

  • Principiante
  • 2 min di lettura
  • 1 esempio eseguibile
Cosa imparerai
  • Where a Rust program starts
  • Printing with println!
  • Why println! ends with !
From source to output
  1. 1main.rsYou write the code
  2. 2rustcThe compiler checks and builds it
  3. 3main()The program starts in main
  4. 4Hello, Rust!The text appears

How a Rust program runs

Every Rust program starts in a function called main. The println! macro prints a line of text; the exclamation mark shows that it is a macro, not an ordinary function.

fn declares a function and the braces hold its body. Each statement ends with a semicolon. The compiler turns the file into a program, then the program runs main and prints the text.

Punti chiave

  1. Every program starts in fn main.
  2. println! is a macro: the ! tells you so.
  3. rustc compiles the file into a program before it runs.

Esempio Rust eseguibile

Rust
Esegui il codice →
main.rs
fn main() {
    println!("Hello, Rust!");
}
Crea un pulsante Run per questo esempio Risultato dell'esecuzione
Hello, Rust!

Esercitati

Apri l'esempio nel compilatore online, cambia un valore o aggiungi una riga e prova a prevedere l'output prima di eseguirlo. Poi rompi il codice di proposito e leggi il messaggio del compilatore.

Hai finito questa lezione?

Segnala come imparata per tenere traccia dei tuoi progressi.

Esercitati con una sfida

Usa ciò che questa lezione spiega per risolvere un problema reale. Ogni sfida gira nel browser, con test immediati e una revisione dell'IA.

Tutte le sfide