Skip to content

Latest commit

History

History
41 lines (32 loc) 路 1.17 KB

03. hello_world.md

File metadata and controls

41 lines (32 loc) 路 1.17 KB

Hello World

fn main() {
    println!("Hello, world!");
}

fn means function. main function is the beginning of every Rust program.
println! prints text to the console and its ! indicate that it鈥檚 a macro instead of a function.

馃挕 Rust files should have .rs file extension and if you鈥檙e using more than one word for the file name, follow the snake_case.

compiling via rustc file.rs
executing by ./file on Linux and Mac or file.exe on Windows


馃挴 These are the other usages of println! macro,

fn main() {
    println!("{}, {}!", "Hello", "world"); // Hello, world!
    println!("{0}, {1}!", "Hello", "world"); // Hello, world!
    println!("{greeting}, {name}!", greeting="Hello", name="world"); // Hello, world!
    
    println!("{:?}", [1,2,3]); // [1, 2, 3]
    println!("{:#?}", [1,2,3]);
    /*
        [
            1,
            2,
            3
        ]
    */
    
    // 馃攷 format! macro is used to store the formatted STRING
    let x = format!("{}, {}!", "Hello", "world");
    println!("{}", x); // Hello, world!
}