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 →

チャレンジで練習する

このレッスンの内容を使って実際の問題を解きましょう。各チャレンジはブラウザで動き、すぐにテストされ、AI のレビューも受けられます。

すべてのチャレンジ