Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Basic Data Types | Rust Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 6 of 50

·rust

Basic Data Types

12 minutes

Course progress12%

Basic Data Types

Rust's type system is a cornerstone of its safety and performance. Every value has a definite type, known at compile time, enabling the compiler to make optimizations and prevent errors.

Scalar Types

Scalars represent single values:

Integers

Signed (can be negative):

  • i8, i16, i32, i64, i128: 8 to 128-bit signed integers
  • isize: Platform-dependent (32-bit on 32-bit systems, 64-bit on 64-bit)

Unsigned (non-negative only):

  • u8, u16, u32, u64, u128: 8 to 128-bit unsigned integers
  • usize: Platform-dependent
let a: i32 = -42;       // Signed 32-bit
let b: u8 = 255;        // Unsigned 8-bit (max value)
let c = 5;              // Type inferred as i32
let d = 1_000_000;      // Underscores for readability

Floating-Point

  • f32: 32-bit IEEE single precision
  • f64: 64-bit IEEE double precision (default)
let pi: f32 = 3.14159;     // 32-bit float
let tau: f64 = 6.283185;    // 64-bit float (default)
let scientific = 1.5e-2;    // 0.015

Boolean

bool values: true or false

let active: bool = true;
let is_raining = false;
let result = 5 > 3;  // true

Characters

char: Single Unicode scalar value (4 bytes)

let letter = 'A';
let emoji = '🦀';  // Emoji are valid!
let special = '\
';  // Escaped characters

Note: char is single-quoted, String is double-quoted.

Compound Types

Tuples

Group multiple values of different types:

let tup: (i32, f64, u8) = (500, 6.4, 1);

// Destructuring
let (x, y, z) = tup;
println!(\"y = {}\", y);  // 6.4

// Accessing by index
println!(\"First: {}\", tup.0);  // 500

Arrays

Fixed-size collections of same type:

let arr: [i32; 3] = [1, 2, 3];  // Type and length specified
let arr = [5; 3];               // [5, 5, 5]
println!(\"First: {}\", arr[0]); // 1

// Arrays have fixed size known at compile time
let len = arr.len();  // 3

Compound Type Example

fn main() {
    // Tuple with mixed types
    let person: (String, i32, bool) = (
        String::from(\"Alice\"),
        30,
        true
    );
    
    let (name, age, active) = person;
    println!(\"{} is {} years old\", name, age);
    
    // Array of numbers
    let scores: [i32; 5] = [95, 87, 92, 88, 90];
    let average = (scores.iter().sum::<i32>() as f64) / scores.len() as f64;
    println!(\"Average: {}\", average);
}

Type Inference

Rust infers types when not explicitly specified:

let x = 5;              // i32 (default integer)
let y = 3.14;           // f64 (default float)
let z = true;           // bool
let values = vec![1, 2, 3];  // Vec<i32>

Type Conversion

Convert between types explicitly:

let x: i32 = 5;
let y: f64 = x as f64;   // Explicit cast

let s = \"42\";
let n: i32 = s.parse().unwrap();  // From string

Integer Overflow

In debug mode, overflow panics. In release mode, it wraps:

let x: u8 = 255;
let y = x + 1;  // Debug: panics; Release: wraps to 0

Use checked operations for safety:

match x.checked_add(1) {
    Some(result) => println!(\"Sum: {}\", result),
    None => println!(\"Overflow!\"),
}
Make it stickPractice what you've learned1 quiz · 1 practical test · Optional activities from Rust FundamentalsBrowse activities
Previous lessonVariables and MutabilityContinue to lesson 7Control FlowBack to Rust Fundamentals curriculum