Rust Strings Quiz

Rust
0 Passed
0% acceptance

40 comprehensive questions on Rust's string types, covering String vs &str, UTF-8 behavior, slicing, mutating strings, and conversions — with 11 code examples demonstrating practical string handling patterns.

40 Questions
~80 minutes
1

Question 1

What are the two main string types in Rust?

A
String and &str
B
str and &String
C
String and string
D
str and Str
2

Question 2

What is the difference between String and &str?

A
String owns data and can grow, &str borrows existing data
B
String is stack-allocated, &str is heap-allocated
C
&str can be modified, String cannot
D
They are identical
3

Question 3

How do you create an empty String?

A
String::new()
B
String::empty()
C
""
D
str::new()
4

Question 4

How do you create a String from a string literal?

A
String::from("hello")
B
"hello".to_string()
C
Both A and B
D
Neither
5

Question 5

What will this code do?

rust
fn main() {
    let s = "hello";
    s.push_str(" world");
    println!("{}", s);
}
A
Print 'hello world'
B
Compilation error
C
Runtime panic
D
Print 'hello'
6

Question 6

How do you append to a String?

A
push_str() method
B
append() method
C
+ operator
D
All of the above
7

Question 7

What does UTF-8 mean for Rust strings?

A
Strings are always valid UTF-8 encoded Unicode
B
Strings use 8-bit encoding only
C
UTF-8 is optional
D
Rust uses UTF-16
8

Question 8

Why can't you index strings with integers?

A
UTF-8 characters vary in byte length
B
Strings are immutable
C
Performance reasons
D
Memory safety
9

Question 9

How do you safely access individual characters?

A
chars() method returns iterator
B
string[index]
C
get() method
D
at() method
10

Question 10

What will this code output?

rust
fn main() {
    let s = "Здравствуйте";
    println!("Length in bytes: {}", s.len());
    println!("Length in chars: {}", s.chars().count());
}
A
Bytes: 24, Chars: 12
B
Bytes: 12, Chars: 12
C
Bytes: 24, Chars: 24
D
Compilation error
11

Question 11

How do you safely slice strings?

A
&string[start..end] with char boundaries
B
string.slice(start, end)
C
substr() method
D
Direct indexing
12

Question 12

What happens if you slice at invalid UTF-8 boundaries?

A
Runtime panic
B
Compilation error
C
Undefined behavior
D
Silent corruption
13

Question 13

How do you convert String to &str?

A
&string or string.as_str()
B
string.to_str()
C
Automatic conversion
D
string.as_ref()
14

Question 14

How do you convert &str to String?

A
to_string() or String::from()
B
Automatic conversion
C
as_string() method
D
Cannot convert
15

Question 15

What is string concatenation using +?

A
Moves the left string, borrows the right
B
Creates new string with both contents
C
Modifies strings in place
D
Only works with literals
16

Question 16

What is the format! macro used for?

A
Creating formatted strings without ownership issues
B
Printing to console
C
Parsing strings
D
String compression
17

Question 17

How do you check if a string contains a substring?

A
contains() method
B
includes() method
C
has() method
D
find() method
18

Question 18

How do you replace text in a string?

A
replace() method creates new string
B
replace() modifies in place
C
substitute() method
D
Cannot replace in strings
19

Question 19

What does trim() do to a string?

A
Removes whitespace from both ends
B
Removes all whitespace
C
Adds whitespace
D
Compresses whitespace
20

Question 20

How do you split a string?

A
split() method returns iterator
B
divide() method
C
split() modifies the string
D
Strings cannot be split
21

Question 21

What is the difference between split() and split_whitespace()?

A
split() uses any separator, split_whitespace() splits on whitespace
B
split_whitespace() is faster
C
They are identical
D
split() only works with single characters
22

Question 22

How do you convert numbers to strings?

A
to_string() method
B
format! macro
C
Both A and B
D
Automatic conversion
23

Question 23

How do you parse strings to numbers?

A
parse() method
B
from_str() function
C
Both A and B
D
Automatic conversion
24

Question 24

What does parse() return?

A
Result<T, ParseIntError>
B
Option<T>
C
T directly
D
String
25

Question 25

How do you handle parse errors?

A
Use expect() or match on Result
B
Use unwrap()
C
Parse errors are impossible
D
Use try! macro
26

Question 26

What is string interning?

A
Storing unique strings in a global table
B
Converting strings to numbers
C
Compressing strings
D
Rust doesn't have string interning
27

Question 27

How do you compare strings in Rust?

A
== operator compares contents
B
== compares references
C
cmp() method
D
equals() method
28

Question 28

What is the capacity of a String?

A
Allocated memory size in bytes
B
Number of characters
C
Maximum string length
D
UTF-8 encoding size
29

Question 29

How do you reserve capacity in a String?

A
reserve() or with_capacity()
B
allocate() method
C
capacity() method
D
Cannot reserve capacity
30

Question 30

What happens when you push to a String at capacity?

A
Automatic reallocation with more capacity
B
Panic
C
Compilation error
D
Data loss
31

Question 31

How do you clear a String?

A
clear() method
B
empty() method
C
reset() method
D
truncate(0)
32

Question 32

What is the difference between clear() and truncate(0)?

A
clear() is optimized, truncate(0) works but is less clear
B
truncate(0) is optimized
C
They are identical
D
clear() removes capacity
33

Question 33

How do you get a substring from a specific position?

A
Use char_indices() to find byte positions, then slice
B
Direct indexing with [start..end]
C
substr() method
D
substring() method
34

Question 34

What does chars().nth(5) return?

A
Option<char> of the 6th character
B
char of the 5th character
C
Compilation error
D
&str of the character
35

Question 35

How do you reverse a string?

A
chars().rev().collect::<String>()
B
reverse() method
C
rev() method
D
Cannot reverse strings
36

Question 36

What is string escaping?

A
Using backslash for special characters
B
Compressing strings
C
Converting to raw strings
D
Removing quotes
37

Question 37

What are raw strings in Rust?

A
r"..." syntax that doesn't process escapes
B
Unprocessed string data
C
Binary string data
D
Rust doesn't have raw strings
38

Question 38

How do you write a multiline string?

A
"line1\nline2" or raw strings with r#"..."#
B
Triple quotes
C
Concatenation only
D
Multiline strings are not supported
39

Question 39

What is the most efficient way to build a string from many parts?

A
Use String with push_str() and reserve capacity
B
Use + operator repeatedly
C
Use format! macro
D
Use collect() on iterator
40

Question 40

When should you prefer &str over String?

A
When you don't need ownership or modification
B
When performance is critical
C
When working with function parameters
D
All of the above

QUIZZES IN Rust