Bases de 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!.
Ce que vous allez apprendre
- Where a Rust program starts
- Printing with println!
- Why println! ends with !
From source to output
- 1
main.rsYou write the code - 2
rustcThe compiler checks and builds it - 3
main()The program starts in main - 4
Hello, 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.
Points clés
- Every program starts in fn main.
- println! is a macro: the ! tells you so.
- rustc compiles the file into a program before it runs.
Exemple Rust exécutable
main.rs
fn main() {
println!("Hello, Rust!");
}
Hello, Rust!S'entraîner
Ouvrez l'exemple dans le compilateur en ligne, modifiez une valeur ou ajoutez une ligne, et devinez la sortie avant de l'exécuter. Cassez ensuite le code exprès et lisez le message du compilateur.
Leçon terminée ?
Marquez-la comme apprise pour suivre votre progression.
Votre progression est enregistrée dans ce navigateur Connectez-vous pour la garder sur tous vos appareils.
S'entraîner avec un défi
Utilisez le contenu de cette leçon pour résoudre un vrai problème. Chaque défi s'exécute dans le navigateur, avec des tests instantanés et une revue par l'IA.
- Impair, Pair ou Aucun des deux Classifiez un nombre selon qu'il s'agit d'un entier impair, d'un entier pair, ou d'aucun des deux. Facile
- Élever un nombre au carré Multiplier un entier par lui-même. Facile