Floats, Booleans, and Chars
let name: f64 = 1.5;
let name: bool = true;
let name: char = 'a';
Floating-Point Numbers
Rust has two floating-point types: f32 (single precision) and f64 (double precision, and the default). They represent numbers with fractional parts, following the IEEE 754 standard.
Example: Floating-Point Numbers
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `price` (a floating-point number), set to `19.99`
let price: f64 = 19.99;
// Declare an immutable variable `discount` (a 32-bit float), set to `0.15`
let discount: f32 = 0.15;
// Print "price: {}, discount: {}" to the console, substituting in `price, discount` (with a trailing newline)
println!("price: {}, discount: {}", price, discount);
}
Login to try C/C++/Java/PHP code in the editor
Boolean Values
The bool type has exactly two possible values, true and false, and is one byte in size. Booleans are most commonly produced by comparison operators and consumed by if expressions.
Example: Boolean Values
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `is_learning`, set to `true`
let is_learning = true;
// Declare an immutable variable `is_finished`, set to `false`
let is_finished = false;
// Print "learning: {}, finished: {}" to the console, substituting in `is_learning, is_finished` (with a trailing newline)
println!("learning: {}, finished: {}", is_learning, is_finished);
}
Login to try C/C++/Java/PHP code in the editor
Character Type
The char type holds a single Unicode scalar value, written in single quotes. Because Rust's char is four bytes, it can represent far more than plain ASCII letters, including emoji and characters from many languages.
Example: Character Type
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `letter`, set to `'R'`
let letter = 'R';
// Declare an immutable variable `emoji`, set to `'🦀'`
let emoji = '🦀';
// Print "letter: {}, emoji: {}" to the console, substituting in `letter, emoji` (with a trailing newline)
println!("letter: {}, emoji: {}", letter, emoji);
}
Login to try C/C++/Java/PHP code in the editor
Comparing Floats Safely
Because floating-point math can introduce tiny rounding errors, comparing two floats with == is risky. A safer approach checks that the difference between them is smaller than a small tolerance value.
Example: Comparing Floats Safely
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `a` (a floating-point number), set to `0.1 + 0.2`
let a: f64 = 0.1 + 0.2;
// Declare an immutable variable `b` (a floating-point number), set to `0.3`
let b: f64 = 0.3;
// Declare an immutable variable `close_enough`, set to `(a - b).abs() < 1e-10`
let close_enough = (a - b).abs() < 1e-10;
// Print "a is close enough to b: {}" to the console, substituting in `close_enough` (with a trailing newline)
println!("a is close enough to b: {}", close_enough);
}
Login to try C/C++/Java/PHP code in the editor
- Comparing floating-point numbers with
==for exact equality, when tiny rounding errors can make them unequal. - Assuming
charin Rust is one byte like in C -- a Rustcharis actually a 4-byte Unicode scalar value. - Forgetting boolean values in Rust are strictly
true/false, not0/1integers like in some other languages.
f32andf64represent floating-point numbers;f64is the default and has more precision.boolvalues are exactlytrueorfalseand are not interchangeable with integers.charrepresents a single Unicode scalar value, written with single quotes, e.g.aor'🦀'.- These are all Rust's basic scalar types, alongside integers.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: