Kinds of Data (Types)
Think about a fruit basket. An apple, a banana, and a grape are all fruit, but theyβre different kinds of fruit. In Rust, the values you store also come in different kinds. We call those kinds types. π’
The everyday types
Here are the ones youβll meet most often:
i32β a whole number, like10or-3. (No decimal point.)f64β a decimal number, like3.14or0.5.boolβ a true-or-false value:trueorfalse.charβ a single character, like'A'or'π¦'. (Use single quotes!)
What about words?
A piece of text is called a string. You write it inside double quotes, like
"hello". There are two flavors youβll hear about: &str (a quick borrowed bit of
text) and String (text you can grow and change). For now, just remember: text goes in
double quotes. π
The little : i32 after a name tells Rust the type. Most of the time Rust can guess
the type on its own, but writing it out makes things extra clear. π
Groups of values: tuples and arrays
Sometimes you want to keep a few values together:
- A tuple holds a small mix of different things:
(10, "ten", true). - An array holds a list of the same kind of thing:
[1, 2, 3].
let pi: f64 = 3.14; and print it.
Press βΆ Run to see your decimal appear!
Quick quiz
Which type would you use for the value true?
Yes! A bool holds a true-or-false value. π
i32), decimals
(f64), true/false (bool), single letters (char),
and text (&str / String). Next up: building little machines
called functions! βοΈ