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
  1. Basics

Variable bindings, constants and statics

⭐️ In Rust variable are immutable by default, so we call them Variable bindings. To make them mutable, mut keyword is used.

⭐️ Rust is a statically typed language; It checks data type at compile time. But it doesn’t require you to actually type it when declare variable bindings. On that case, the compiler checks the usage and sets a better data type for it. But for constants and statics you must annotate the type. Types come after a colon(:)

  • Variable bindings

let a = true;
let b: bool = true;

let (x, y) = (1, 2);

let mut z = 5;
z = 6;
  • Constants

const N: i32 = 5;
  • Statics

static N: i32 = 5;

let keyword is used in binding expressions. We can bind a name to a value or a function. Also because the left-hand side of a let expression is a ‘pattern’, you can bind multiple names to set of values or function values.

const keyword is used to define constants. It lives for the entire lifetime of a program but have no fixed address in memory. static keyword is used to define ‘global variable’ type facility. There is only one instance for each value, and it’s at a fixed location in memory.

💡 Always use const, instead of static. It’s pretty rare that you actually want a memory location associated with your constant, and using a const allows for optimizations like constant propagation not only in your crate but also in downstream crates.

💡 Usually statics are placed at top of the code file, outside the functions.

PreviousComments and documenting the codeNextFunctions

Last updated 7 years ago