HelloWorld Example in Rust Language
Here’s a simple “Hello, World!” example in Rust:
page_title -->fn main() {
println!("Hello, World!");
}
Here’s what this code does:
fn main() { ... }
: This is the entry point of the Rust program. Every Rust program must have amain
function.println!("Hello, World!");
: This line prints the “Hello, World!” message to the console. Theprintln!
macro is used to format and print text to the standard output.
To run this code:
- Save it to a file with a
.rs
extension, for example,hello.rs
. - Open a terminal.
- Navigate to the directory where you saved the
hello.rs
file using thecd
command. - Compile the Rust program using the
rustc
command:
rustc hello.rs
- Run the compiled program:
./hello
You should see the “Hello, World!” message printed to the console. This is a basic example of a Rust program, and it demonstrates how to define a main
function and print output to the console.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.