📖 Scenario: Imagine you are creating a simple program to store and display a person's full name. Understanding how strings work and how they are stored in memory is important for this task.
🎯 Goal: You will create a string variable to hold a full name, add a middle name to it, and then print the full name. This will help you understand how strings are created, modified, and displayed in Rust.
📋 What You'll Learn
Create a string variable with a first and last name
Add a middle name to the existing string
Print the final full name
💡 Why This Matters
🌍 Real World
Manipulating strings is common in programs that handle user names, messages, or any text data.
💼 Career
Understanding string basics and memory helps in writing efficient and bug-free code in Rust, which is valuable for systems programming and backend development.
Progress0 / 4 steps
1
Create a String with First and Last Name
Create a String variable called full_name and set it to the value "John Doe".
DSA Rust
Hint
Use String::from to create a new string in Rust.
2
Add a Middle Name to the String
Add the middle name "Michael " to the beginning of the full_name string using the insert_str method.
DSA Rust
Hint
The insert_str method inserts a string slice at a specified position.
3
Print the Full Name
Use println! to print the full_name string.
DSA Rust
Hint
Use println!("{}", variable) to print a variable's value.
4
Run and Verify the Output
Run the program and verify that the output is exactly Michael John Doe.
DSA Rust
Hint
Check the console output matches the expected full name.
Practice
(1/5)
1. What does the String type in Rust represent?
easy
A. A growable, heap-allocated string
B. A fixed-size array of characters
C. A pointer to a string literal
D. An immutable string slice
Solution
Step 1: Understand Rust string types
Rust has two main string types: String and string slices (&str). String is growable and stored on the heap.
Step 2: Compare options with definitions
A growable, heap-allocated string correctly describes String as growable and heap-allocated. Others describe slices or fixed arrays.
Final Answer:
A growable, heap-allocated string -> Option A
Quick Check:
String = growable heap string [OK]
Hint: Remember: String is growable and heap-allocated [OK]
Common Mistakes:
Confusing String with &str (string slice)
Thinking String is fixed size
Assuming String is immutable
2. Which of the following is the correct way to create a new empty String in Rust?
easy
A. let s = String::new();
B. let s = String();
C. let s = new String();
D. let s = String[];
Solution
Step 1: Recall Rust String creation syntax
In Rust, String::new() creates a new empty String.
Step 2: Check each option syntax
let s = String::new(); uses correct Rust syntax. The other options use invalid Rust syntax for creating a String.
Final Answer:
let s = String::new(); -> Option A
Quick Check:
String::new() creates empty String [OK]
Hint: Use String::new() to create empty String [OK]
Common Mistakes:
Using parentheses like a function call without ::new()
Trying to use new keyword like other languages
Using array brackets for String
3. What will be the output of this Rust code?
let s = String::from("hello");
let slice = &s[0..2];
println!("{}", slice);
medium
A. Runtime error
B. hel
C. hello
D. he
Solution
Step 1: Understand string slicing in Rust
The slice &s[0..2] takes characters from index 0 up to but not including 2.
Step 2: Extract substring from "hello"
Characters at index 0 and 1 are 'h' and 'e', so the slice is "he".
Final Answer:
he -> Option D
Quick Check:
Slice 0..2 of "hello" = "he" [OK]
Hint: Slice 0..n means characters from 0 up to n-1 [OK]
Common Mistakes:
Assuming slice includes index 2
Expecting full string output
Thinking slicing causes runtime error
4. Identify the error in this Rust code snippet:
let s = String::from("hello");
let slice = &s[0..4];
println!("{}", slice);
let ch = &s[4];
medium
A. Slice range 0..4 is invalid
B. Cannot index String with single usize
C. Missing semicolon after println!
D. String::from is not valid
Solution
Step 1: Check indexing rules for String
Rust does not allow indexing a String with a single usize like s[4] because strings are UTF-8 encoded and indexing by byte is unsafe.
Step 2: Verify other parts
Slice &s[0..4] is valid. Semicolons and String::from usage are correct.
Final Answer:
Cannot index String with single usize -> Option B
Quick Check:
Single index on String causes error [OK]
Hint: Use slices, not single index, to access String parts [OK]
Common Mistakes:
Trying to access String by single index
Confusing slice range validity
Ignoring Rust UTF-8 string rules
5. Given a UTF-8 encoded Rust String containing emoji and letters, which method safely returns the number of characters (not bytes)?
hard
A. s.len()
B. s.as_bytes().len()
C. s.chars().count()
D. s.bytes().count()
Solution
Step 1: Understand difference between bytes and characters
UTF-8 strings store characters as 1 or more bytes. len() returns byte count, not character count.
Step 2: Identify method counting characters
s.chars().count() iterates over Unicode scalar values (characters) and counts them correctly.
Final Answer:
s.chars().count() -> Option C
Quick Check:
chars().count() counts characters, not bytes [OK]
Hint: Use chars().count() for character count, not len() [OK]
Common Mistakes:
Using len() which counts bytes, not characters
Confusing bytes() with chars()
Assuming as_bytes().len() counts characters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.