Ad space (banner)
Lesson 4 / 20

Type annotations and basic types

This lesson covers Rust's basic types: i32, f64, bool, and &str.

Rust's type inference is strong enough that you can usually leave types out, but you can also annotate them explicitly by writing name: type. The most common are i32 (32-bit integer), f64 (64-bit floating point), and bool.

The sample code annotates its variables explicitly, as in let score: i32 = 85; and let average: f64 = 72.5;.

A common early stumble is trying to mix an integer type (i32) and a floating-point type (f64) in one calculation. Rust does not convert between different types automatically — you have to convert explicitly.

In professional work, spelling types out gives you fine control over the range of a number and how much memory it uses.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Rust directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)