liveBook · Manning

Bytes

As explained in Learn Rust in a Month of Lunches

A byte is a fixed-size unit of digital information consisting of eight bits in conventional modern systems.

A byte is eight bits. Each bit can be 0 or 1. Rust integer type names show their size in bits: `u8` uses one byte, while `i64` uses eight bytes. section 1.3

What fits in one byte?

The eight bits of a `u8` can all be set to 1. Their values add up to 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255, the largest `u8` value. section 1.3

fn main() {
    let str1 = "Hello!";
    println!("str1 is {} bytes and also {} characters.", str1.len(), str1.chars().count());
    let str2 = "안녕!";
    println!("str2 is {} bytes but only {} characters.", str2.len(), str2.chars().count());
}

Try the string-length example: `.len()` counts bytes, while `.chars().count()` counts characters. `"Hello!"` has six of each; `"안녕!"` has seven bytes but three characters. section 1.3

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines a byte as eight bits, works through binary values, and demonstrates byte counts without detailing encoding rules.
Examples
Compares `u8` with larger integers and counts the bytes and characters in `"Hello!"` and `"안녕!"`.
What it assumes
Introduces bits and binary through a comparison with decimal numbers while using simple Rust expressions.
What you can do after
Evaluate/understand why an integer’s type determines its size and a string’s byte length can differ from its character count.
How it connects
Connects bytes to bits, binary numbers, integer types, strings, and character counts.

Also explained in

Compare how 7 books explain bytes →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.