Learning Rust
gitbook
gitbook
  • Introduction
  • Basics
    • Why Rust
    • Installation
    • Hello World
    • Cargo,crates and basic project structure
    • Comments and documenting the code
    • Variable bindings, constants and statics
    • Functions
    • Primitive data types
    • Operators
    • Control flows
  • Beyond The Basics
    • Vectors
    • Structs
    • Enums
    • Generics
    • Impls and traits
  • The Tough Part
    • Ownership
    • Borrowing
    • Lifetimes
  • Lets Get It Started
    • Code organization
    • Functions
    • Modules
    • Crates
    • Workspaces
    • use
    • std, primitives and preludes
Powered by GitBook
On this page
  • Hello, World!
  • Rust Playground
  • Usages of println!
  1. Basics

Hello World

PreviousInstallationNextCargo,crates and basic project structure

Last updated 7 years ago

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’s a instead of a function.

💡 Rust files should have .rs file extension and if you’re using more than one word for the file name, follow the .

  • Save above code in file.rs , but it can be any name with .rs extension.

  • Compiling via rustc file.rs

  • Executing by ./file on Linux and Mac or file.exe on Windows

Rust Playground

is a web interface for running Rust code.

Usages of println!

💯 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!
}
macro
snake_case
Rust Playground
Rust Playground