Basic

Primitive Types

Understand Rust's built-in types: integers, floats, strings, and booleans.

What you will see

42

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

You will print different numeric types, compare owned and borrowed strings, and convert between types.

1

I store values in three different integer types and print them. Each size suits a different range of numbers.

fn main() { let a: i32 = 42; let b: u8 = 7; let c: usize = 100; println!("{} {} {}", a, b, c); }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
42 7 100
2

I print a float and a boolean. Rust has no implicit numeric conversion, so each type stays distinct.

fn main() { let price: f64 = 9.99; let active = true; println!("{} {}", price, active); }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
9.99 true
3

I create an owned String and a borrowed &str, then print both. Both types display the same text.

fn main() { let owned = String::from("hello"); let borrowed = "world"; println!("{} {}", owned, borrowed); }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
hello world
4

I parse a string into an integer and print it. The parse method turns the text into a number.

fn main() { let s = "42"; let n: i32 = s.parse().unwrap(); println!("{}", n); }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
42
unwrap() panics if the string cannot be parsed.
5

I convert a number back into a string with to_string and print it. Converting the other way requires parse.

fn main() { let n = 42; let back = n.to_string(); println!("{}", back); }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
42

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A literal is stored in a typed slot; conversion re-encodes it into a new type.
1 Literal stored in a typed variable
↓
2 Conversion requested with parse()/to_string()
↓
3 The value is re-encoded
↓
4 The new value is used
Why this works
Integer types

Rust has many integer sizes:

TypeSignedBytes
i8yes1
i16yes2
i32yes4
usizeunsignedpointer size

usize is for sizes and indices. i32 is the default choice for counting.

Type conversion

Convert explicitly with methods:

let s = "42";
let n: i32 = s.parse().unwrap();
let back = n.to_string();

parse() converts a string into a number; unwrap() extracts the value or panics on failure. to_string() converts a number back to a string.

Examples

Integers

Whole numbers

let a: i32 = 42;
let b: u8 = 7;
let c: usize = 100;

i32 is the default integer. i8/i16/i64 choose signed sizes; u8/u16/u32/u64 are unsigned. usize matches the pointer size and is used for indexing.

Why it works: choosing the right size saves memory and makes intent clear.

Floats and booleans

Decimals and logic

let price: f64 = 9.99;
let active = true;

f64 is the default float. bool is exactly true or false. Rust has no implicit numeric conversion, you must convert explicitly.

Watch out: Rust strings are UTF-8 bytes; you cannot index a string by position.

String types

Owned vs borrowed

let owned = String::from("hello");
let borrowed = "world";   // &str

String is owned and heap-allocated; &str is a borrowed reference to text. Most functions take &str so they accept both.

Why it works: &str is a view into a slice of bytes with a known length.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Variables and Immutability
Previous Lesson
Variables and Immutability
Next Lesson
Conditionals and Matching
Conditionals and Matching →