Bird
Raised Fist0
Rustprogramming~5 mins

String slices in Rust - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a string slice in Rust?
A string slice is a reference to a part of a string. It does not own the data but points to a section of a string, allowing you to work with parts of strings without copying.
Click to reveal answer
beginner
How do you create a string slice from a String in Rust?
You can create a string slice by using the syntax &string[start..end], where start and end are byte indices. For example, &my_string[0..5] gives a slice of the first 5 bytes.
Click to reveal answer
intermediate
Why must string slices use valid UTF-8 boundaries in Rust?
Because Rust strings are UTF-8 encoded, slicing must happen at valid character boundaries to avoid invalid UTF-8 data, which would cause runtime panics.
Click to reveal answer
beginner
What is the difference between a String and a string slice (&str) in Rust?
A String owns its data and can be modified, while a string slice (&str) is a reference to a part of a string and cannot be changed. String slices are lightweight views into strings.
Click to reveal answer
beginner
Can you store a string slice in a variable? How?
Yes, you can store a string slice in a variable by declaring it with type &str. For example: let slice: &str = &my_string[0..4];
Click to reveal answer
What does a string slice (&str) in Rust represent?
AAn owned string that can be modified
BA reference to part of a string
CA number representing string length
DA pointer to a character array in C
Which syntax correctly creates a string slice of the first 3 characters of a String named 'text'?
Atext.slice(0,3)
B&text[3..0]
C&text[0..3]
Dtext[0..3]
Why must string slices be on valid UTF-8 boundaries?
ATo avoid runtime errors from invalid UTF-8 data
BTo improve performance
CTo allow modification of the string
DTo convert to ASCII automatically
Which of these is true about &str in Rust?
AIt owns the string data
BIt can be resized
CIt is a mutable string buffer
DIt is immutable and references string data
How do you declare a variable holding a string slice?
Alet s: &str = &my_string[0..5];
Blet s: String = &my_string[0..5];
Clet s = my_string.slice(0,5);
Dlet s = &my_string.to_string();
Explain what a string slice is in Rust and how it differs from a String.
Think about ownership and mutability differences.
You got /5 concepts.
    Describe how to create a string slice from a String and why slicing must respect UTF-8 boundaries.
    Consider how Rust stores strings and safety rules.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is a string slice in Rust?
      easy
      A. A type of integer used for indexing strings
      B. A reference to a part of a string without copying it
      C. A function that modifies a string in place
      D. A new string created by copying characters

      Solution

      1. Step 1: Understand string slice concept

        A string slice is a reference to a part of a string, not a new string.
      2. Step 2: Compare options

        Only A reference to a part of a string without copying it describes a reference without copying, which matches string slices.
      3. Final Answer:

        A reference to a part of a string without copying it -> Option B
      4. Quick Check:

        String slice = reference without copy [OK]
      Hint: Remember: slices borrow, not copy strings [OK]
      Common Mistakes:
      • Thinking slices create new strings
      • Confusing slices with string modification
      • Believing slices are integer types
      2. Which of the following is the correct syntax to get a slice of the first 4 characters from a string text?
      easy
      A. &text[0..4]
      B. text.slice(0, 4)
      C. &text[1..5]
      D. text[0:4]

      Solution

      1. Step 1: Recall Rust slice syntax

        Rust uses &string[start..end] to get a slice from start to end-1.
      2. Step 2: Check options

        &text[0..4] uses &text[0..4], which correctly slices first 4 chars (indices 0 to 3).
      3. Final Answer:

        &text[0..4] -> Option A
      4. Quick Check:

        Slice syntax = &string[start..end] [OK]
      Hint: Use &string[start..end] for slices, not methods or colons [OK]
      Common Mistakes:
      • Using method calls like slice() which don't exist
      • Using 1-based indexing instead of 0-based
      • Using Python-style slicing with colons
      3. What will the following Rust code print?
      let s = String::from("hello world");
      let slice = &s[6..11];
      println!("{}", slice);
      medium
      A. hello
      B. Syntax error
      C. hello world
      D. world

      Solution

      1. Step 1: Understand slicing indices

        The slice &s[6..11] takes characters from index 6 up to 10 (11 excluded).
      2. Step 2: Identify substring

        In "hello world", index 6 is 'w' and index 10 is 'd', so slice is "world".
      3. Final Answer:

        world -> Option D
      4. Quick Check:

        Slice &s[6..11] = "world" [OK]
      Hint: Remember slice end index is excluded [OK]
      Common Mistakes:
      • Including the end index character
      • Confusing start and end indices
      • Expecting full string output
      4. Identify the error in this Rust code snippet:
      let s = String::from("rustacean");
      let slice = &s[0..5];
      println!("{}", slice);
      let invalid_slice = &s[5..15];
      medium
      A. The slice &s[5..15] causes a runtime panic due to out-of-bounds
      B. The slice &s[0..5] is invalid syntax
      C. The println! macro is missing a semicolon
      D. String::from cannot create strings with length 9

      Solution

      1. Step 1: Check slice ranges

        The string "rustacean" has length 9, so valid indices are 0 to 8.
      2. Step 2: Analyze invalid slice

        The slice &s[5..15] tries to access indices beyond length, causing runtime panic.
      3. Final Answer:

        The slice &s[5..15] causes a runtime panic due to out-of-bounds -> Option A
      4. Quick Check:

        Out-of-bounds slice = panic [OK]
      Hint: Slice end must be ≤ string length to avoid panic [OK]
      Common Mistakes:
      • Assuming slices auto-adjust to string length
      • Thinking &s[0..5] is invalid syntax
      • Ignoring runtime panics from invalid slices
      5. Given a string text, how can you safely get a slice of the first n characters without risking a runtime panic if n is larger than the string length?
      hard
      A. Use text.get(0..n).unwrap()
      B. Use &text[0..n] without checks
      C. Use &text[0..n.min(text.len())]
      D. Use &text[0..=n]

      Solution

      1. Step 1: Understand safe slicing

        To avoid panic, slice end must not exceed string length.
      2. Step 2: Use min to limit slice end

        Using n.min(text.len()) ensures slice end is at most string length.
      3. Final Answer:

        Use &text[0..n.min(text.len())] -> Option C
      4. Quick Check:

        Safe slice end = min(n, length) [OK]
      Hint: Use min() to limit slice end safely [OK]
      Common Mistakes:
      • Slicing without length check causing panic
      • Using unwrap() which can panic if None
      • Using inclusive range which is invalid for slices
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard