This flow shows how to take a part of a String by choosing start and end positions and getting a slice that points to that part.
Execution Sample
Rust
let s = String::from("hello world");
let slice = &s[0..5];
println!("{}", slice);
This code takes the first 5 characters of the string and prints "hello".
Execution Table
Step
Code Line
Action
Value/Result
1
let s = String::from("hello world");
Create a String with value
"hello world"
2
let slice = &s[0..5];
Take slice from index 0 to 5 (exclusive)
"hello"
3
println!("{}", slice);
Print the slice
Output: hello
4
End
Program ends
No more actions
💡 Program ends after printing the slice.
Variable Tracker
Variable
Start
After Step 1
After Step 2
Final
s
uninitialized
"hello world"
"hello world"
"hello world"
slice
uninitialized
uninitialized
"hello"
"hello"
Key Moments - 3 Insights
Why does &s[0..5] give "hello" and not include the space?
Because the slice takes characters from index 0 up to but not including index 5, which covers 'h','e','l','l','o'. The space is at index 5 and is excluded. See execution_table row 2.
Can we change the slice to modify the original String?
No, slices are references to parts of the String and are read-only. To modify, you need the original String. The slice is &str, immutable. See variable_tracker for slice type.
What happens if indices are out of bounds?
Rust will panic at runtime if slice indices are out of bounds. Always ensure indices are valid. This example uses valid indices as shown in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'slice' after step 2?
A"world"
B"hello "
C"hello"
D"hello world"
💡 Hint
Check execution_table row 2 under Value/Result column.
At which step does the program print the slice?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for println! action in execution_table.
If we change slice to &s[6..11], what would be printed?
A"world"
B"hello world"
C"hello"
D"world!"
💡 Hint
Check variable_tracker and remember slice indices are start inclusive, end exclusive.
Concept Snapshot
String slices in Rust let you borrow parts of a String without copying.
Syntax: &string[start..end] where end is exclusive.
Slices are &str, immutable views into the original String.
Indices must be valid UTF-8 boundaries.
Use slices to work with parts of strings efficiently.
Full Transcript
This example shows how to create a String in Rust and then take a slice of it using &s[0..5]. The slice contains the characters from index 0 up to but not including 5, which results in "hello". The program then prints this slice. The slice is a reference to part of the original String and is immutable. If indices are invalid, Rust will panic. This trace helps visualize each step: creating the String, slicing it, and printing the slice.
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
Step 1: Understand string slice concept
A string slice is a reference to a part of a string, not a new string.
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.
Final Answer:
A reference to a part of a string without copying it -> Option B
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
Step 1: Recall Rust slice syntax
Rust uses &string[start..end] to get a slice from start to end-1.
Step 2: Check options
&text[0..4] uses &text[0..4], which correctly slices first 4 chars (indices 0 to 3).
Final Answer:
&text[0..4] -> Option A
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
Step 1: Understand slicing indices
The slice &s[6..11] takes characters from index 6 up to 10 (11 excluded).
Step 2: Identify substring
In "hello world", index 6 is 'w' and index 10 is 'd', so slice is "world".
Final Answer:
world -> Option D
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
Step 1: Check slice ranges
The string "rustacean" has length 9, so valid indices are 0 to 8.
Step 2: Analyze invalid slice
The slice &s[5..15] tries to access indices beyond length, causing runtime panic.
Final Answer:
The slice &s[5..15] causes a runtime panic due to out-of-bounds -> Option A
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
Step 1: Understand safe slicing
To avoid panic, slice end must not exceed string length.
Step 2: Use min to limit slice end
Using n.min(text.len()) ensures slice end is at most string length.
Final Answer:
Use &text[0..n.min(text.len())] -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.