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

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 6 of 50 · rust

Basic Data Types

Duration: 12 minutes

Basic Data Types\n\nRust'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.\n\n## Scalar Types\n\nScalars represent single values:\n\n### Integers\n\nSigned (can be negative):\n- i8, i16, i32, i64, i128: 8 to 128-bit signed integers\n- isize: Platform-dependent (32-bit on 32-bit systems, 64-bit on 64-bit)\n\nUnsigned (non-negative only):\n- u8, u16, u32, u64, u128: 8 to 128-bit unsigned integers\n- usize: Platform-dependent\n\nrust\nlet a: i32 = -42; // Signed 32-bit\nlet b: u8 = 255; // Unsigned 8-bit (max value)\nlet c = 5; // Type inferred as i32\nlet d = 1_000_000; // Underscores for readability\n\n\n### Floating-Point\n\n- f32: 32-bit IEEE single precision\n- f64: 64-bit IEEE double precision (default)\n\nrust\nlet pi: f32 = 3.14159; // 32-bit float\nlet tau: f64 = 6.283185; // 64-bit float (default)\nlet scientific = 1.5e-2; // 0.015\n\n\n### Boolean\n\nbool values: true or false\n\nrust\nlet active: bool = true;\nlet is_raining = false;\nlet result = 5 > 3; // true\n\n\n### Characters\n\nchar: Single Unicode scalar value (4 bytes)\n\nrust\nlet letter = 'A';\nlet emoji = '🦀'; // Emoji are valid!\nlet special = '\\n'; // Escaped characters\n\n\nNote: char is single-quoted, String is double-quoted.\n\n## Compound Types\n\n### Tuples\n\nGroup multiple values of different types:\n\nrust\nlet tup: (i32, f64, u8) = (500, 6.4, 1);\n\n// Destructuring\nlet (x, y, z) = tup;\nprintln!(\"y = {}\", y); // 6.4\n\n// Accessing by index\nprintln!(\"First: {}\", tup.0); // 500\n\n\n### Arrays\n\nFixed-size collections of same type:\n\nrust\nlet arr: [i32; 3] = [1, 2, 3]; // Type and length specified\nlet arr = [5; 3]; // [5, 5, 5]\nprintln!(\"First: {}\", arr[0]); // 1\n\n// Arrays have fixed size known at compile time\nlet len = arr.len(); // 3\n\n\n## Compound Type Example\n\nrust\nfn main() {\n // Tuple with mixed types\n let person: (String, i32, bool) = (\n String::from(\"Alice\"),\n 30,\n true\n );\n \n let (name, age, active) = person;\n println!(\"{} is {} years old\", name, age);\n \n // Array of numbers\n let scores: [i32; 5] = [95, 87, 92, 88, 90];\n let average = (scores.iter().sum::<i32>() as f64) / scores.len() as f64;\n println!(\"Average: {}\", average);\n}\n\n\n## Type Inference\n\nRust infers types when not explicitly specified:\n\nrust\nlet x = 5; // i32 (default integer)\nlet y = 3.14; // f64 (default float)\nlet z = true; // bool\nlet values = vec![1, 2, 3]; // Vec<i32>\n\n\n## Type Conversion\n\nConvert between types explicitly:\n\nrust\nlet x: i32 = 5;\nlet y: f64 = x as f64; // Explicit cast\n\nlet s = \"42\";\nlet n: i32 = s.parse().unwrap(); // From string\n\n\n## Integer Overflow\n\nIn debug mode, overflow panics. In release mode, it wraps:\n\nrust\nlet x: u8 = 255;\nlet y = x + 1; // Debug: panics; Release: wraps to 0\n\n\nUse checked operations for safety:\n\nrust\nmatch x.checked_add(1) {\n Some(result) => println!(\"Sum: {}\", result),\n None => println!(\"Overflow!\"),\n}\n

Previous: Variables and MutabilityNext: Control Flow