Rullst AcademyRullst Academy/Lesson
Rust II: data, choices and confidence · Lesson 03 / 12

Text is more than single-byte letters

The word ação has four letters but occupies six UTF-8 bytes. Choosing the right measure avoids invalid cuts.

Learning paths10 min

Let's discover together

  1. 1

    String owns UTF-8 text and can grow. &str is a borrowed view of a valid UTF-8 sequence; a function can read without taking ownership.

  2. 2

    len() measures bytes. chars().count() counts Unicode scalar values. In ação: a and o use one byte each; ç and ã use two each.

  3. 3

    A visual character can combine several scalar values, such as a letter with a combining accent or some emoji. chars().count() is not a universal count of reader-perceived characters.

  4. 4

    String does not offer integer indexing to a letter. A byte slice needs valid UTF-8 boundaries. Prefer suitable operations such as split_whitespace for words or get for a slice that may fail.

FROM LESSON TO PRACTICE

A guide to follow at your pace

Read the steps before using the reference below. Commands run in the indicated system’s terminal; signup happens on the official site. Rullst Academy exercises are practice and do not verify your installation or external account.

fn main() {
    let text = String::from("ação");
    println!("{} {}", text.len(), text.chars().count());
}

Official links and next step

✦
YOUR MISSION

Distinguish String, &str, bytes and Unicode scalar values.

Now it's your turn

Discovery workshop

Read, predict, investigate

Predict before revealing. This panel shows prepared results; it does not compile your code. Compiler errors are summarised; exact wording can vary with the compiler version.
Mini experiment

Learning simulation: compare cards and check your predictions. The browser is not compiling Rust or starting a server.

fn main() {
    let text = String::from("ação");
    println!("{} {}", text.len(), text.chars().count());
}
Build your answers and check when you are ready.
A moment to understand

Does chars().count() always count visual symbols?

I need a hint

Bytes, scalar values and visual characters are different measures.

Loading your journey…

Take it with you

Change the text to café and then an emoji. Record bytes and chars. Try text.get(0..2) on ação and explain why it can return None without crashing.

RUST II · .rs

Your Rust II workbench

Read, predict and answer here. To actually compile, extract the activity ZIP into a new folder, open a terminal beside Cargo.toml and use the commands below. Rust and your operating system tools must be installed.

cargo check --offline
cargo test --offline
cargo run --offline
My files and GitHub

The editor below saves a draft in this browser and lets you download or publish your .rs. To run it in the project, save the edited file as src/main.rs. The ZIP contains the starter version; prepared results do not change with your edits. Online grading does not verify local execution.

At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.