Published Apr 18, 2024
Here's a simple "Hello, world!" program in Rust:
fn main() {
println!("Hello, world!");
}
Let's break it down:
fn main()
: This defines the main
function, which is the entry point for your program. Every Rust program needs a main
function.println!("Hello, world!");
: This line uses the println!
macro to print the string "Hello, world!" to the console. The !
at the end indicates it's a macro, not a regular function.To run this program, you'll need Rust installed on your system. Here are the steps:
main.rs
.rustc main.rs
This will compile the code and generate an executable file (usually named main
or main.exe
depending on your OS).
3. Run the program: Execute the generated file:
./main // For Linux and macOS
./main.exe // For Windows
If everything works correctly, you should see "Hello, world!" printed on your terminal.
This is a very basic example, but it demonstrates the core structure of a Rust program. You can find more information and tutorials on writing Rust programs in the official Rust documentation https://doc.rust-lang.org/rust-by-example/hello.html.