Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Learn RustChapter 2: Values

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. i32 is a whole number that can be negative or positive, like a score that can drop below zero. usize is 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 a usize.
  • Decimal numbers. f64 is a number with a fractional part, such as an average message length of 42.5 bytes. Chatterbox does not need one yet, but you should recognise it.
  • True or false. bool has exactly two values, true and false. It is a switch: on or off, yes or no.
Writing the type after the name
fn main() {
let max_len: usize = 200;
let average_len: f64 = 42.5;
let quiet: bool = false;
println!("{} {} {}", max_len, average_len, quiet);
}
$ cargo run
200 42.5 false

The : 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.

PredictDivide two whole numbers
fn main() {
println!("{}", 7 / 2);
}

What does this print?

  • 3
  • 3.5
  • 4
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.

Decimals keep the fraction
fn main() {
println!("{}", 7.0 / 2.0);
}
$ cargo run
3.5

Comparing 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.

Questions with yes/no answers
fn main() {
let length = 250;
let max_len = 200;
println!("{}", length > max_len);
println!("{}", length == max_len);
}
$ cargo run
true
false

Combining 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.

not, and
fn main() {
let length = 250;
let max_len = 200;
let quiet = false;
println!("{}", !quiet);
println!("{}", length > 0 && length <= max_len);
}
$ cargo run
true
false

Read 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 after lesson 2.3: the whole program (green lines are new in this lesson)
src/main.rs3 new lines
// Chatterbox: a small chat program, built one lesson at a time.
 
/// The first line Chatterbox prints.
fn banner(bot: &str, version: &str) -> String {
format!("{} v{}", bot, version)
}
 
/// The greeting, with a name filled in.
fn greeting(name: &str, bot: &str) -> String {
format!("Hi {}! I'm {}.", name, bot)
}
 
fn main() {
let name = "Sam";
let bot = "Chatterbox";
let version = "0.2";
let mut messages_seen = 0;
let max_len: usize = 200; // the longest message we accept, in bytes
let quiet = false; // true would skip the greeting
 
println!("{}", banner(bot, version));
println!("{}", greeting(name, bot));
println!("Type a message and I will answer.");
println!("(limit: {} bytes a message, quiet: {})", max_len, quiet);
 
// The greeting was our first message, so the count goes up by one.
messages_seen += 1;
println!("messages so far: {}", messages_seen);
}
Chatterbox prints (a real run)
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.

Try itYour turn: too long?

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.

fn is_too_long(max_len: usize, length: usize) -> bool {
// true only when length is more than max_len
false
}
 
fn main() {
println!("{}", is_too_long(200, 250));
}

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.

Self-check

Say it out loud, then reveal

Self-checkRetrieval beats re-reading
What is 7 / 2 for whole numbers, and why?Reveal
3. Whole numbers have no fraction, so the remainder is dropped.
Which type would you pick for the longest message Chatterbox accepts, and why?Reveal
usize: it is a size, a whole number that can never be negative, and it is the type Rust uses for lengths and counts.
What is the difference between = and ==?Reveal
= gives a name a value. == compares two values and answers true or false.
What do <code>!</code>, <code>&amp;&amp;</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.

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.

Learn Rust by building a chat app · Lesson 2.3 · every compiler message above is real output from rustc 1.99.0 (b940084d7 2026-09-28) (edition 2024) · Vibe Engines · 2026