Lesson 2.3 · 8 min
Numbers and true/false
Chatterbox needs a token budget, a temperature and a quiet switch. Those are three different kinds of value, and Rust wants to know which kind each one is. That is a feature: it catches silly mistakes before the program runs.
Adds to ChatterboxA token budget and a "quiet" switch.
Every value has a type
So far every value was text. Values come in kinds, and the kind is called the type. Rust checks types before your program runs, so asking text to behave like a number is caught early. Three kinds cover most of what Chatterbox needs.
- Whole numbers.
u32is a whole number that cannot be negative (the u is for unsigned).i32is a whole number that can be. A token budget isu32: you cannot have minus ten tokens. - Decimal numbers.
f64is a number with a fractional part, such as a temperature of0.7. - True or false.
boolhas exactly two values,trueandfalse.
$ cargo run
4096 0.7 falseThe : u32 after the name is the type. You can often leave it out, and Rust works it out from the value: a bare 0 becomes i32, a bare 0.7 becomes f64. Writing it is clearer when the choice matters, such as u32 for a budget.
Arithmetic, and one surprise
The usual symbols work: +, -, *, /. The surprise is in dividing whole numbers. A whole number has no room for a fraction, so the fraction is dropped.
What does this print?
33.54
Reveal the answer
The program prints: 3 Both sides are whole numbers, so the answer is a whole number: 7 divided by 2 is 3 with some left over, and the leftover is dropped. To get 3.5, at least one side must be a decimal, as in 7.0 / 2.0.
$ cargo run
3.5Comparing gives a bool
A bool usually comes from a comparison. > and < ask which is bigger, == asks whether two things are equal, and != asks whether they differ. Each answers with true or false.
$ cargo run
true
falseRight now a bool is only printed. In chapter 3 it decides what Chatterbox does next. Note that = assigns and == compares; they are different symbols on purpose.
Chatterbox v0.2, step three
The bot gets a token budget and a quiet switch, and prints both.
$ cargo run
Chatterbox v0.2
Hi Sam! I'm Chatterbox.
1 messages so far
Budget: 4096 tokens, quiet: falseMake is_over_budget answer true only when used is strictly more than budget. Replace the placeholder with a comparison. You don't need to understand fn yet (chapter 3, lesson 4 explains it): just fill in the body.
Make is_over_budget answer true only when used is strictly more than budget. Replace the placeholder with a comparison. You don't need to understand fn yet (chapter 3, lesson 4 explains it): just fill in the body.
Hidden tests (3): exactly_at_budget_is_fine over under
This exercise runs real Rust in your browser through the official Rust Playground, so it needs JavaScript.
Say it out loud, then reveal
What is 7 / 2 for whole numbers, and why?Reveal
Which type would you pick for a token budget, and why?Reveal
What is the difference between = and ==?Reveal
That is lesson 2.3. Tick it off so the course remembers where you are.
Questions people ask
What is the difference between i32 and u32?
Both are 32-bit whole numbers. i32 can be negative; u32 cannot, which lets it reach twice as high.
Which number type does Rust choose by default?
i32 for whole numbers and f64 for decimals, when nothing else says otherwise.
Can I add a whole number and a decimal?
Not directly. Rust treats them as different types and asks you to convert one on purpose.
Sources The Rust Book: data types