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

Built-in Types and Variables

What you’ll learn: How Rust primitives, strings, mutability, and conversions differ from Java’s type model.

Difficulty: 🟢 Beginner

Rust looks familiar at first because it has integers, booleans, strings, and variables. The differences start showing up once ownership, mutability, and explicit conversions enter the picture.

Primitive Types

JavaRustNotes
inti32explicit width
longi64explicit width
doublef64default floating-point choice
booleanboolsame general role
charcharUnicode scalar value, not UTF-16 code unit
byteu8 or i8choose signedness explicitly

Rust forces width and signedness into the spelling. That removes guesswork at API boundaries.

Variables and Mutability

#![allow(unused)]
fn main() {
let name = "Ada";
let mut count = 0;
count += 1;
}

Bindings are immutable by default. This is one of the earliest places where Rust asks for more explicit intent than Java.

Shadowing

#![allow(unused)]
fn main() {
let port = "8080";
let port: u16 = port.parse().unwrap();
}

Shadowing lets a name be rebound with a new type or refined value. It is often cleaner than introducing parsedPort-style names everywhere.

String vs &str

This is the first string distinction Java developers must really learn.

Rust typeRough Java intuitionMeaning
Stringowned Stringheap-allocated, owned text
&strread-only string viewborrowed string slice

If a function only needs to read text, prefer &str.

Formatting and Printing

#![allow(unused)]
fn main() {
let name = "Ada";
let score = 42;
println!("{name} scored {score}");
}

Rust formatting uses macros rather than overloaded println methods.

Explicit Conversions

Rust avoids many implicit numeric conversions:

#![allow(unused)]
fn main() {
let x: i32 = 10;
let y: i64 = x as i64;
}

That can feel verbose at first, but it reduces accidental widening and narrowing.

Advice

  • Use immutable bindings unless mutation is genuinely needed.
  • Prefer &str for input parameters and String for owned returned text.
  • Read the type annotations in compiler diagnostics carefully; they are often the fastest way to learn.

This chapter is where the surface syntax still feels easy. The harder conceptual shift begins when values start moving rather than merely being referenced.