0Pricing
Learn Rust Coding · Lesson

Scalar Types

Integers, floats, bool, char.

Scalar Types is a free Learn Rust Coding lesson on CoddyKit — lesson 2 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Scalar Types?

A scalar type represents a single value. Rust has four primary scalar types:

  • Integers
  • Floating-point numbers
  • Booleans
  • Characters
fn main() {
    let i = 10;       // integer
    let f = 2.5;      // float
    let b = true;     // bool
    let c = 'R';      // char
    println!("{} {} {} {}", i, f, b, c);
}

Integer Types

Integers come in signed (i) and unsigned (u) variants with fixed sizes: i8, i16, i32, i64, i128, and the same for u.

The default integer type is i32.

fn main() {
    let small: i8 = -120;
    let big: u64 = 18_000_000_000;
    println!("{} {}", small, big);
}

Signed vs Unsigned

Signed integers can be negative; unsigned cannot.

  • i8 ranges from -128 to 127.
  • u8 ranges from 0 to 255.

Choose unsigned when a value can never be negative, like a count.

fn main() {
    let temperature: i32 = -15;
    let people: u32 = 250;
    println!("temp {} people {}", temperature, people);
}

Integer Literals

You can write integers in different bases and use underscores for readability.

  • Decimal: 1_000_000
  • Hex: 0xff
  • Octal: 0o77
  • Binary: 0b1010
fn main() {
    let dec = 1_000;
    let hex = 0xff;
    let bin = 0b1010;
    println!("{} {} {}", dec, hex, bin);
}

Floating-Point Types

Rust has two float types: f32 and f64. The default is f64 because it offers more precision.

Floats follow the IEEE-754 standard.

fn main() {
    let pi: f64 = 3.141592;
    let e: f32 = 2.718;
    println!("pi {} e {}", pi, e);
}

Numeric Operations

Rust supports the usual math operators: +, -, *, /, and % (remainder).

Integer division truncates toward zero.

fn main() {
    let sum = 5 + 10;
    let quotient = 7 / 2;     // 3 (integer)
    let remainder = 7 % 2;    // 1
    let float_div = 7.0 / 2.0; // 3.5
    println!("{} {} {} {}", sum, quotient, remainder, float_div);
}

The Boolean Type

The bool type has exactly two values: true and false.

Booleans are produced by comparisons and used in if conditions.

fn main() {
    let is_active = true;
    let is_bigger = 10 > 3;
    println!("{} {}", is_active, is_bigger);
}

The Character Type

A char represents a single Unicode scalar value and is written with single quotes.

A Rust char is 4 bytes and can hold far more than ASCII — emoji and accented letters included.

fn main() {
    let letter = 'A';
    let symbol = '@';
    let heart = '\u{2764}';
    println!("{} {} {}", letter, symbol, heart);
}

char vs String

Single quotes make a char; double quotes make a string slice (&str).

  • 'a' is a single character.
  • "a" is a one-character string.
fn main() {
    let c = 'x';
    let s = "x";
    println!("char {} string {}", c, s);
}

Type Conversion with as

Rust does not convert numeric types automatically. Use the as keyword for explicit casts.

Be careful: casting to a smaller type can lose data.

fn main() {
    let x: i32 = 65;
    let y: f64 = x as f64;
    let c = x as u8 as char;
    println!("{} {} {}", x, y, c);
}

Putting Scalars Together

Programs combine scalar types constantly. Here we mix integers, floats, and booleans to make a decision.

fn main() {
    let price: f64 = 19.99;
    let quantity: u32 = 3;
    let total = price * quantity as f64;
    let is_expensive = total > 50.0;
    println!("total {} expensive {}", total, is_expensive);
}

Quick Check

Test your knowledge of Rust scalar literals.

Recap

Rust's four scalar types:

  • Integers — signed (i32) and unsigned (u32), various sizes.
  • Floats — f32 and f64, default f64.
  • bool — true or false.
  • char — a single Unicode value in single quotes.

Use as for explicit numeric conversions.

fn main() {
    let count: u32 = 42;
    let ratio: f64 = count as f64 / 7.0;
    let ok: bool = ratio > 5.0;
    let grade: char = 'B';
    println!("{} {} {} {}", count, ratio, ok, grade);
}

Frequently asked questions

Is the “Scalar Types” lesson free?

Yes — the full text of “Scalar Types” is free to read here on the web, and the Learn Rust Coding course includes 4 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 “Scalar Types”?

Integers, floats, bool, char. 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 4, so you can start here or from the beginning and move at your own pace.

How long does the “Scalar Types” 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. let and Mutability
  2. Scalar Types
  3. Compound Types
  4. Shadowing
← Back to Learn Rust Coding