Back to TILs

Rust helloworld

Table of contents

Installation on Debian 11

Install dependencies

sudo apt install curl build-essential gcc make -y

Install Rust

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Install code formatter

rustup component add rustfmt

Format code

cargo fmt
# or
rustfmt lib.rs main.rs
# or
echo "fn     main() {}" | rustfmt
fn main() {
    // Indent four space by convention
    println!("Hello, rust!");
    //     ^
    // the ! indicates you are calling a macro instead a function
    let x = 10;
    // x = 20; // compilation error because x is immutable

    let mut y = 10;
    println!("x: {} y: {}", x, y);

    y = 20; // ok
    println!("x: {} y: {}", x, y);
}

Possible output

Hello, rust!
x: 10 y: 10
x: 10 y: 20

References