Lesson 2.3 · 8 min
Numbers and true/false
Chatterbox needs a limit on how long a message may be, and a quiet switch. A length is a number, a switch is yes or no, and Rust wants to know which kind each value is. That is a feature: it catches silly mistakes before the program runs.
Adds to ChatterboxA length limit 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.
i32is a whole number that can be negative or positive, like a score that can drop below zero.usizeis a whole number used for sizes and counts: how many bytes long a message is (a byte is the small unit computers measure memory in; one plain English letter takes one byte), how many messages there are. It can never be negative, because nothing is minus three bytes long, and Rust uses it whenever it tells you how big something is. Chatterbox's length limit is ausize. - Decimal numbers.
f64is a number with a fractional part, such as an average message length of42.5bytes. Chatterbox does not need one yet, but you should recognise it. - True or false.
boolhas exactly two values,trueandfalse. It is a switch: on or off, yes or no.
$ cargo run
200 42.5 falseThe : usize 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 42.5 becomes f64, and false is plainly a bool. Writing it is clearer when the choice matters, such as usize for a length. (Why there are so many whole-number types, and how big each one can get, is the whole of the next lesson.)
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
falseCombining yes/no answers
Three more symbols work on bool values themselves. ! means not: it flips true to false and back, so !quiet reads "not quiet". && means and: it is true only when both sides are true. || means or: it is true when at least one side is.
$ cargo run
true
falseRead them aloud. "Not quiet" is true. "Longer than nothing and within the limit" is false, because 250 is over the limit, and one false side is enough to make && false. And quiet || length > max_len, "quiet or too long", would be true here, because one true side is enough for ||. (Rust works out the comparisons first and the && or || after, so no brackets are needed.)
Right 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
Two new settings go into Chatterbox. max_len is the longest message it will accept, 200 bytes, written as a usize because it is a size. quiet is a switch that, once Chatterbox can make decisions, will skip the greeting when it is true. Neither changes anything yet; a new line prints both so you can see them.
Chatterbox v0.2 Hi Sam! I'm Chatterbox. Type a message and I will answer. (limit: 200 bytes a message, quiet: false) messages so far: 1
Download this version (zip)Unzip, then cargo run inside the folder.
Notice that quiet has no type written after it. Rust can see that false is a bool. For max_len we wrote usize on purpose. A bare 200 with nothing else to go on becomes an i32, and in chapter 3 we compare the limit with a message's length, which Rust measures as a usize. Writing the type says what the number is for.
Make is_too_long answer true only when length is strictly more than max_len. A message exactly at the limit is fine. Replace the placeholder false with a comparison.
Make is_too_long answer true only when length is strictly more than max_len. A message exactly at the limit is fine. Replace the placeholder false with a comparison.
Hidden tests (4): empty_message_is_never_too_long exactly_at_the_limit_is_fine one_byte_over short_message
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 the longest message Chatterbox accepts, and why?Reveal
What is the difference between = and ==?Reveal
What do <code>!</code>, <code>&&</code> and <code>||</code> do?Reveal
! flips a bool (not). && is true only when both sides are true (and). || is true when at least one side is true (or).That is lesson 2.3. Tick it off so the course remembers where you are.
Go deeper
Questions people ask
What is usize in Rust?
A whole number used for sizes and counts, such as how many bytes long some text is. It can never be negative, and its size matches the computer: 64 bits on most machines today.
What is the difference between i32 and usize?
i32 is a whole number that can be negative. usize cannot be negative and is the type Rust uses for lengths, counts and positions.
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