Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Language Basics  ›  Lesson

Strings, Tuples and Arrays

String 12 minString is owned, growable text while &str is a borrowed view of text, a tuple groups a fixed set of values of different types, and an array holds a fixed number of values of one type.

Strings, Tuples and Arrays

A catalogue entry is more than one number. It has a title (text), maybe a title and page count travelling together, and a week of daily loan counts. Rust has a specific type for each shape, and text comes in two kinds that everyone mixes up at first.

The rule

String owns text, &str borrows it; tuples mix types, arrays repeat one.

String is text the variable owns. It lives on the heap and can grow.

let mut title = String::from("Iron Tides");
title.push_str(" (2nd edition)");

&str (a "string slice") is a borrowed view into text stored somewhere else. Every literal in quotes is a &str, and it is read-only.

let shelf_name: &str = "Fiction";
let first_word: &str = &title[0..4];   // "Iron"

Convert between them: "Fiction".to_string() or String::from(...) makes a String; &title gives a &str view of a String. Functions that only read text should take &str, so they accept both.

Strings are UTF-8, so .len() counts bytes, not characters. "café".len() is 5. Walk characters with .chars(). For the same reason you cannot write title[0]; Rust forbids indexing a string by position.

Tuples group a fixed number of values that may differ in type. Read them by position:

let entry: (&str, u32) = ("Night Garden", 288);
println!("{} has {} pages", entry.0, entry.1);

Arrays hold a fixed number of values of one type. The length is part of the type:

let week: [u32; 7] = [4, 2, 5, 3, 6, 9, 1];
let monday = week[0];
let blank = [0; 7];            // seven zeros

Indexing past the end, like week[7], panics instead of reading stray memory.

The shortcut

Ask two questions: Is it text? Then "do I need to own or change it?" Yes → String, no → &str. Not text? "Same type every time?" Yes → array, no → tuple.

Typing a catalogue lookupStep 1 of 4
  1. The search term

    Text the function only reads. &str.

Drills

Answer fast — trust the rule.

quick drill

What type is the literal "Fiction"?

quick drill

What does "café".len() return?

quick drill

After let entry = ("Night Garden", 288);, how do you read 288?

quick drill

Which type holds exactly seven u32 values?

Remember

  • String owns and can grow; &str borrows and reads. Functions that only read take &str.
  • Text? Own or change it → String, else &str. Not text? Same type → array, mixed → tuple.
  • Trap: .len() counts bytes, and strings cannot be indexed by position.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainString or &str, tuple or array?

The library program stores a book title that a user can edit, returns a (title, pages) pair from a lookup, and keeps exactly seven daily loan counts for a week. Explain which type fits each and why, including the difference between String and &str.