0Pricing
Learn Rust Coding · Lesson

Primitive Data Types and Operators

Explore Rust's built-in primitive data types like integers, floats, booleans, and characters, along with common operators.

Primitive Data Types and Operators is a free Learn Rust Coding lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro to Primitive Data Types

Welcome to a foundational lesson in Rust! Today, we'll explore primitive data types and basic operators.

Primitive types are the most fundamental building blocks for storing data. They represent simple values like numbers, true/false, or single characters.

Understanding these types is crucial for writing efficient and correct Rust code.

Rust's Integer Types

Rust provides various integer types to fit different needs. They come in two main flavors:

  • Signed Integers (i prefix): Can store both positive and negative numbers (e.g., i8, i32, i64).
  • Unsigned Integers (u prefix): Can only store positive numbers or zero (e.g., u8, u32, u64).

The number indicates the bits of storage, e.g., i32 is a 32-bit signed integer. isize and usize are pointer-sized integers, adapting to your system's architecture (32-bit or 64-bit).

Declaring Integer Variables

Let's see how to declare and use integer types in Rust. Notice how we can explicitly add a type annotation or let Rust infer it.

Try running this example:

fn main() {
  let signed_val: i32 = -100;
  let unsigned_val: u32 = 250;
  let inferred_int = 500; // Rust infers i32 by default
  let big_unsigned: u64 = 1_000_000_000;

  println!("Signed: {}", signed_val);
  println!("Unsigned: {}", unsigned_val);
  println!("Inferred: {}", inferred_int);
  println!("Big Unsigned: {}", big_unsigned);
}

Floating-Point Numbers

For numbers with decimal points, Rust offers floating-point types:

  • f32: Single-precision float (32-bit).
  • f64: Double-precision float (64-bit). This is the default floating-point type in Rust.

f64 is generally preferred for most applications due to its higher precision, unless you have specific memory or performance constraints.

Declaring Float Variables

Here's how to declare floating-point numbers. Notice that Rust defaults to f64 if you don't specify a type.

Try running this example:

fn main() {
  let pi: f32 = 3.14159;
  let e = 2.718281828; // Inferred as f64
  let temperature: f64 = 98.6;

  println!("Pi (f32): {}", pi);
  println!("E (f64 inferred): {}", e);
  println!("Temperature (f64): {}", temperature);
}

Booleans and Characters

Beyond numbers, Rust has types for logical values and single letters:

  • bool: Represents a truth value, either true or false.
  • char: Represents a single Unicode scalar value. This means a char can be more than just ASCII – it can be any letter, emoji, or symbol.

A char uses 4 bytes of memory, ensuring it can store any Unicode character.

Bools and Chars in Action

Let's declare some boolean and character variables. Notice the single quotes for char values.

Try running this example:

fn main() {
  let is_rust_fun: bool = true;
  let has_errors = false; // Inferred as bool
  let first_letter: char = 'R';
  let emoji_char: char = '🚀';

  println!("Is Rust fun? {}", is_rust_fun);
  println!("Has errors? {}", has_errors);
  println!("First letter: {}", first_letter);
  println!("Emoji: {}", emoji_char);
}

Basic Arithmetic Operators

Just like in math, Rust uses operators to perform calculations. Here are the common arithmetic operators:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Remainder (Modulo)

Remember that integer division (e.g., 10 / 3) will truncate the decimal part, resulting in an integer (3, not 3.33).

Using Arithmetic Operators

Let's put these operators into practice with some numbers. Pay attention to how integer division behaves!

Try running this example:

fn main() {
  let a = 15;
  let b = 4;

  println!("Addition: 15 + 4 = {}", a + b);
  println!("Subtraction: 15 - 4 = {}", a - b);
  println!("Multiplication: 15 * 4 = {}", a * b);
  println!("Integer Division: 15 / 4 = {}", a / b);
  println!("Remainder: 15 % 4 = {}", a % b);

  let x = 15.0;
  let y = 4.0;
  println!("Float Division: 15.0 / 4.0 = {}", x / y);
}

Primitive Types Quiz

Test your understanding of Rust's primitive data types and basic operators.

Recap: Primitives & Operators

Great job! You've learned about Rust's fundamental data types:

  • Integers: i8, u8, i32, u32, etc., for whole numbers.
  • Floats: f32 and f64 for decimal numbers.
  • Booleans: bool for true or false.
  • Characters: char for single Unicode values.

You also explored basic arithmetic operators like +, -, *, /, and %. These are the building blocks for more complex logic in your programs.

Frequently asked questions

Is the “Primitive Data Types and Operators” lesson free?

Yes — the full text of “Primitive Data Types and Operators” is free to read here on the web, and the Learn Rust Coding course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Primitive Data Types and Operators”?

Explore Rust's built-in primitive data types like integers, floats, booleans, and characters, along with common operators. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Primitive Data Types and Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Variables, Mutability, and Shadowing
  2. Primitive Data Types and Operators
  3. Functions and Control Flow
← Back to Learn Rust Coding