Основы 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!.

  • Начальный
  • Чтение: 2 мин
  • Примеров для запуска: 1
Что вы узнаете
  • 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.

Главное

  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.

Пример на Rust, который можно запустить

Rust
Запустить код →
main.rs
fn main() {
    println!("Hello, Rust!");
}
Получить кнопку запуска для этого примера Результат выполнения
Hello, Rust!

Практика

Откройте пример в онлайн-компиляторе, измените значение или добавьте строку и попробуйте предсказать вывод до запуска. Затем намеренно сломайте код и прочитайте сообщение компилятора.

Закончили этот урок?

Отметьте его как изученный, чтобы следить за прогрессом.

Далее: Variables and Mutability →

Попрактикуйтесь на задаче

Примените материал этого урока к реальной задаче. Каждая задача запускается в браузере, с мгновенными тестами и разбором от ИИ.

Все задачи