Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1.2 Variables, Data Types, Constants

Variables

// main.rs

fn main() {
    // creation
    let a = 5;
    //   ^ here type is annotated implicitly
    let b: i16 = 10;
    //    ^^^^ here type is explicitly annotated as i16.
    // if we try to assign 5.0 float point to b, it will
    // be an error because of type annotation.

    // mutability
    let c = 10; // variables are immutable as default in Rust
    let mut d = 20;
    // ^^^^ 'mut' keyword is used to make variable mutable.
    d = 120; // now we can change the value because of mutability

    // shadowing
    let e = 10;
    let e = 110; // the variable 'e' is shadowed.
    println!("{e}"); // 110 will be printed out to the terminal.
}

Data Types

// main.rs

fn main() {
    // Scalar Data Types which store single data type

    // boolean
    let b1: bool = true;

    // unsigned integers
    let i1: u8 = 1;
    let i2: u16 = 1;
    let i3: u32 = 1;
    let i4: u64 = 1;
    let i5: u128 = 1;

    // signed integers
    let i6: i8 = 1;
    let i7: i16 = 1;
    let i9: i32 = 1;
    let i10: i64 = 1;
    let i11: i128 = 1;

    // floating point numbers
    let f1: f32 = 1.0;
    let f2: f64 = 1.0;

    // platform specific integers
    let p1: isize = 1; // represents pointer sized signed integer
    let p2: usize = 1; // represents pointer sized unsigned integer

    // characters, &str, and String
    let c1: char = 'c';
    let s1: &str = "Rust";
    let s2: String = String::from("Rust");


    // Compound Data Types which store multiple data types

    // arrays
    let a: [i32, 5] = [1, 2, 3, 4, 5]; // all members have the same type
    let a1 = a[4]; // reach the any element of array by "variable[<index>]"

    // tuples
    let t1: (i32, i32, i32) = (5, 10, 15);
    // each member can have different type
    let t2: (i32, f64, &str) = (5, 10.5, "rust");

    let s1 = t2.2; // reach the any element by "variable.<index>"
    let (i1: i32, f1: f64, s1: &str) = t2; // the tuple can also be destructured

    let t1 = (); // empty tuple is the special tuple type called "unit type"


    // Type Aliasing
    type model = u16;
    let model_year: model = 1996;
}

Constants

ConstantStatic
SCREAMING_SNAKE_CASESCREAMING_SNAKE_CASE
Explicit type annotationExplicit type annotation
Can not be mutatedCan be marked as mutated
Value of it will be inlined at compile timeIt occupies location in the memory
// main.rs
const MAX_LIMIT: u8 = 100; // inlined where it is used
//              ^^^^ explicit type annotation needed
static MIN_NUMBER: i32 = -5; // occupy location in the memory
//                ^^^^ explicit type annotation needed
//    ^^^^^^^^^^^ SCREAMING_SNAKE_CASE needed

fn main() {

}

Operators in Rust

CategoryOperatorsDescriptionExample
Arithmetic+, -, *, /, %Basic math operations5 + 3, 10 % 4
Comparison==, !=, <, >, <=, >=Compare values, return boola == b, x < y
Logical&&, !Boolean logic operationstrue && false, !is_valid
Bitwise&, ^, <<, >>Bit-level operations on integersx & y, 1 << 3
Assignment=, +=, -=, *=, /=, %=, etc.Assign and update valuescount += 1, x = 42
Reference&Borrowing (create reference)let r = &value;
Dereference*Follow a reference to access valuelet x = *ptr;
Range.., ..=Range expressions (exclusive/inclusive)for i in 0..5 {}, 1..=3
Error Propagation?Return early if Result::Err or Option::Nonelet v = might_fail()?;
CastingasType conversionlet x = y as f64;