Rust book: write, compile, and run a Hello, world! program
Answer: Now that you've installed Rust, it is time to write your first Rust program.
A first project
Now that you've installed Rust, write your first Rust program in a hello_world directory under ~/projects, or %USERPROFILE%\projects on Windows CMD.
The source file is main.rs. Rust files always end with the .rs extension. If a filename has more than one word, the convention is to use an underscore.
The program is a main function that calls println! with Hello, world!. On Linux or macOS you compile with rustc main.rs and start the binary with ./main. On Windows the run command is .\main.
What the program is doing
main is special: it is the first function that runs in an executable. This one takes no parameters and returns nothing. Parameters would be written inside the parentheses.
The body sits in curly brackets. It is good style to place the opening curly bracket on also the same line as the function declaration, with one space in between.
This line prints text. It is a Rust macro, not a function; a function would be written println without the !. A ! means you are calling a macro, and macros don't always follow the same rules as functions.
Compile, then run
You compile first with rustc and the file name, such as rustc main.rs.
You then run ./main or .\main on Windows.
See also
Claims
- id: C1 claim: Rust source files use the .rs suffix. source: S2 evidence: "Rust files always end with"
- id: C2 claim: main is the first function that runs in an executable. source: S1 evidence: "always the first code that runs in every executable"
- id: C3 claim: println! is a macro, not a function. source: S2 evidence: "you're calling a macro instead of a normal"
- id: C4 claim: rustc main.rs writes a binary executable. source: S1 evidence: "rustc main.rs"
- id: C5 claim: Rust is compiled ahead of time. source: S2 evidence: "ahead-of-time compiled"
Sources
- [S1] Hello, World!. The Rust Project Developers. https://doc.rust-lang.org/book/ch01-02-hello-world.html . License: MIT OR Apache-2.0. Accessed 2026-11-27.
- [S2] Hello, World! (source). The Rust Project Developers. https://raw.githubusercontent.com/rust-lang/book/main/src/ch01-02-hello-world.md . License: MIT OR Apache-2.0. Accessed 2026-11-27.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Hello, World!; The Rust Project Developers, Hello, World! (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Hello, World!; The Rust Project Developers, Hello, World! (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers.
Cite this
iinventedlight. 2026. "Rust book: write, compile, and run a Hello, world! program." iinventedlight Library, iil:note:5gh1a4zw, version of 2026-11-27. https://iinventedlight.com/library/notes/rust-hello-world/. License: CC-BY-4.0.
@misc{iil_note_5gh1a4zw,
author = {{iinventedlight}},
title = {{Rust book: write, compile, and run a Hello, world! program}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-27},
url = {https://iinventedlight.com/library/notes/rust-hello-world/},
note = {Permanent ID iil:note:5gh1a4zw. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-hello-world.schema.json (schema; CC-BY-4.0)
- rust-hello-world.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-hello-world.md.