Bird
Raised Fist0
DSA Rustprogramming~20 mins

Why Strings Are a Data Structure Not Just Text in DSA Rust - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
String Structure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why are strings considered a data structure in Rust?
Which of the following best explains why strings in Rust are considered a data structure rather than just plain text?
ABecause strings in Rust only store ASCII characters and ignore others.
BBecause strings in Rust are immutable and cannot be changed once created.
CBecause strings in Rust are just arrays of characters with no extra information.
DBecause strings in Rust store characters along with metadata like length and capacity, allowing efficient operations.
Attempts:
2 left
πŸ’‘ Hint
Think about what extra information a string holds besides just characters.
❓ Predict Output
intermediate
2:00remaining
Output of Rust String length and capacity
What is the output of this Rust code snippet?
DSA Rust
let s = String::from("hello");
println!("Length: {}, Capacity: {}", s.len(), s.capacity());
ALength: 5, Capacity: 5
BLength: 5, Capacity: 10
CLength: 6, Capacity: 5
DLength: 5, Capacity: 0
Attempts:
2 left
πŸ’‘ Hint
String::from allocates exactly the byte length needed, so capacity equals length for this ASCII string.
πŸ”§ Debug
advanced
3:00remaining
Why does this Rust code panic when slicing a string?
Consider this Rust code: let s = String::from("ЗдравствуйтС"); let slice = &s[0..4]; println!("{}", slice); Why does this code panic at runtime?
ABecause the slice range does not align with UTF-8 character boundaries, causing an invalid slice.
BBecause the string is empty and cannot be sliced.
CBecause Rust strings cannot be sliced using ranges.
DBecause the slice range is out of bounds of the string length.
Attempts:
2 left
πŸ’‘ Hint
Rust strings are UTF-8 encoded; slicing must respect character boundaries.
πŸš€ Application
advanced
3:00remaining
Choosing the right string type for efficient manipulation in Rust
You need to build a program that frequently appends characters to a text buffer. Which Rust string type is best suited for this task?
Achar, because it stores a single character and can be appended repeatedly.
BString, because it is growable and stores UTF-8 data with capacity management.
CVec<u8>, because it stores bytes and is not related to strings.
D&str, because it is a fixed-size string slice and more efficient for appending.
Attempts:
2 left
πŸ’‘ Hint
Think about which type allows growth and efficient appending.
❓ Predict Output
expert
4:00remaining
Output of Rust string mutation and capacity after push_str
What is the output of this Rust code?
DSA Rust
let mut s = String::with_capacity(5);
s.push_str("hi");
println!("Length: {}, Capacity: {}", s.len(), s.capacity());
s.push_str(" there");
println!("Length: {}, Capacity: {}", s.len(), s.capacity());
A
Length: 2, Capacity: 5
Length: 8, Capacity: 5
B
Length: 2, Capacity: 5
Length: 8, Capacity: 15
C
Length: 2, Capacity: 5
Length: 8, Capacity: 10
D
Length: 2, Capacity: 5
Length: 8, Capacity: 8
Attempts:
2 left
πŸ’‘ Hint
Capacity grows when length exceeds current capacity, usually doubling.

Practice

(1/5)
1. Why are strings considered a data structure and not just plain text in Rust?
easy
A. Because strings cannot be changed once created
B. Because strings are only used for printing text
C. Because strings store characters in a sequence with indexing and length properties
D. Because strings are always fixed size arrays

Solution

  1. Step 1: Understand what a data structure is

    A data structure organizes data to allow operations like indexing, length checking, and iteration.
  2. Step 2: Analyze string properties in Rust

    Rust strings store characters in order, support length queries, and allow access to parts, so they behave like a data structure.
  3. Final Answer:

    Because strings store characters in a sequence with indexing and length properties -> Option C
  4. Quick Check:

    Strings = Data structure [OK]
Hint: Strings hold ordered characters with length and indexing [OK]
Common Mistakes:
  • Thinking strings are just plain text without structure
  • Assuming strings are immutable fixed arrays
  • Confusing strings with single characters
2. Which of the following is the correct way to create a mutable String in Rust?
easy
A. let mut s = String::new();
B. let s = 'hello';
C. let s = String::new();
D. let s = String::from("hello");

Solution

  1. Step 1: Identify mutable string creation syntax

    In Rust, to create a mutable String, you declare it with mut and initialize with String::new() or String::from().
  2. Step 2: Verify the correct syntax

    The correct way is let mut s = String::new();, which creates an empty mutable String. Without mut the String is immutable. Single quotes denote characters, not strings, and String::from() requires a string slice like "hello".
  3. Final Answer:

    let mut s = String::new(); -> Option A
  4. Quick Check:

    Mutable String = let mut s = String::new(); [OK]
Hint: Use 'mut' keyword to make String mutable [OK]
Common Mistakes:
  • Forgetting 'mut' keyword for mutability
  • Using single quotes for strings instead of double quotes
  • Confusing &str with String type
3. What will be the output of this Rust code?
let mut s = String::from("hello");
s.push_str(", world!");
println!("{}", s);
medium
A. "hello"
B. "hello, world!"
C. Compilation error
D. "hello world"

Solution

  1. Step 1: Understand push_str method

    The push_str method appends a string slice to the existing String.
  2. Step 2: Trace the code execution

    Initially, s = "hello". After s.push_str(", world!"), s becomes "hello, world!". The print statement outputs this full string.
  3. Final Answer:

    "hello, world!" -> Option B
  4. Quick Check:

    push_str appends text = "hello, world!" [OK]
Hint: push_str adds text to String, so output includes appended text [OK]
Common Mistakes:
  • Expecting original string without appended text
  • Confusing push_str with push (which adds single char)
  • Thinking code causes compile error
4. Identify the error in this Rust code snippet:
let s = String::from("hello");
s.push('!');
medium
A. Cannot push a character to an immutable String
B. push method does not exist for String
C. Missing semicolon after push
D. String::from cannot create a String

Solution

  1. Step 1: Check mutability of String

    The variable s is declared without mut, so it is immutable.
  2. Step 2: Understand push method requirements

    The push method modifies the String by adding a character, which requires the String to be mutable.
  3. Final Answer:

    Cannot push a character to an immutable String -> Option A
  4. Quick Check:

    push needs mutable String = error if not mutable [OK]
Hint: Make String mutable with 'mut' before push [OK]
Common Mistakes:
  • Ignoring mut keyword requirement
  • Thinking push is invalid method
  • Assuming missing semicolon causes error
5. Given a String in Rust, how can you efficiently count the number of characters (not bytes) it contains?
hard
A. Use s.capacity() to find character count
B. Use s.len() to get the character count
C. Use s.bytes().count() to count characters
D. Convert to chars iterator and count elements with s.chars().count()

Solution

  1. Step 1: Understand difference between bytes and characters

    Rust strings store UTF-8 bytes; len() returns byte count, not character count.
  2. Step 2: Use chars iterator to count characters

    The chars() method returns an iterator over Unicode scalar values (characters). Counting these gives the correct character count.
  3. Final Answer:

    Convert to chars iterator and count elements with s.chars().count() -> Option D
  4. Quick Check:

    chars().count() = character count [OK]
Hint: Use s.chars().count() for accurate character count [OK]
Common Mistakes:
  • Using s.len() which counts bytes, not characters
  • Using bytes().count() which counts bytes
  • Confusing capacity() with length
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