- Basic Rust Program:
fn main() { println!("Hello, world!"); } - Compile & Run:
rustc main.rsthen./main - Key Tools:
rustup: Manages Rust versions and associated toolsrustc: The Rust compiler that compiles source code into executable binary
- Key Syntax:
println!is a macro (note the!), functions usefn, statements end with;
Install Rust using rustup, which is the recommended way to install Rust. It manages Rust versions and associated tools.
rustupcommands:rustup update: Updates Rust to the latest version.rustup doc: Opens the Rust documentation in the browser.
main.rs:
fn main() {
println!("Hello, world!");
}To run the program, use the following commands in your terminal:
rustc main.rs # Compiles the Rust code
./main # Runs the compiled Rust programThis will output:
Hello, world!
- The
mainfunction is the entry point of a Rust program. - Rust uses 4 spaces for indentation.
println!is a macro that prints text to the console. The!indicates that it is a macro, not a function.- In this stage, let's just understand that macros and functions are a bit different.
;is used to denote the end of a statement in Rust.rustcis the Rust compiler that compiles the source code into an executable binary.- We will use
cargofor managing projects and dependencies later, which simplifies this process instead of usingrustcdirectly.
- We will use