Skip to content

Latest commit

History

History
executable file
62 lines (46 loc) 路 2.07 KB

a3.hello_world.md

File metadata and controls

executable file
62 lines (46 loc) 路 2.07 KB

title: Hello World

Hello, World!

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

fn means function. The main function is the beginning of every Rust program.
println!() prints text to the console and its ! indicates that it鈥檚 a macro rather than 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 convention.

  • Save the above code in file.rs , but it can be any name with .rs extension.
  • Compile it with rustc file.rs
  • Execute it with ./file on Linux and Mac or file.exe on Windows

Rust Playground

Rust Playground is a web interface for running Rust code.

Rust Playground

Before going to the next...

  • These are the other usages of the 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!

    let (greeting, name) = ("Hello", "world"); // 馃挕 Two Variable bindings declare & initialize in one line.
    println!("{greeting}, {name}!"); // Hello, world!

    println!("{:?}", [1, 2, 3]); // [1, 2, 3]
    println!("{:#?}", [1, 2, 3]);
    /*
        [
            1,
            2,
            3
        ]
    */

    // 馃攷 The format! macro is used to store the formatted string.
    let x = format!("{}, {}!", "Hello", "world");
    println!("{}", x); // Hello, world!

    // 馃挕 Rust has a print!() macro as well
    print!("Hello, world!"); // Without new line
    println!(); // A new line

    print!("Hello, world!\n"); // With new line
}