Fundamentos 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!.

  • Principiante
  • 2 min de lectura
  • 1 ejemplo ejecutable
Lo que aprenderás
  • 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.

Puntos clave

  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.

Ejemplo ejecutable de Rust

Rust
Ejecutar código →
main.rs
fn main() {
    println!("Hello, Rust!");
}
Obtén un botón Run para este ejemplo Resultado de ejecutar el código
Hello, Rust!

Practica

Abre el ejemplo en el compilador online, cambia un valor o añade una línea y predice la salida antes de ejecutarlo. Después rompe el código a propósito y lee el mensaje del compilador.

¿Terminaste esta lección?

Márcala como aprendida para seguir tu progreso.

Practica con un reto

Usa lo que cubre esta lección para resolver un problema real. Cada reto se ejecuta en el navegador, con pruebas al instante y una revisión de IA.

Todos los retos