Introduction
The String
class in Rust provides a growable, mutable, UTF-8 encoded string type. It is commonly used for dynamically allocated strings that can be modified or concatenated.
Using String
The String
class can be used to create and manipulate string data. Here is a basic example:
fn main() {
let mut s = String::new();
s.push_str("Hello");
s.push_str(", world!");
println!("{}", s);
}
// Output: Hello, world!
Key Methods
Below are some of the key methods exposed by the String
class:
new
Creates a new, empty String
.
fn main() {
let s = String::new();
println!("Created an empty string: '{}'", s);
}
// Output: Created an empty string: ''
from
Creates a String
from a string slice.
fn main() {
let s = String::from("Hello, Rust!");
println!("{}", s);
}
// Output: Hello, Rust!
push_str
Appends a string slice to the String
.
fn main() {
let mut s = String::from("Hello");
s.push_str(", world!");
println!("{}", s);
}
// Output: Hello, world!
push
Appends a single character to the String
.
fn main() {
let mut s = String::from("Hello");
s.push('!');
println!("{}", s);
}
// Output: Hello!
len
Returns the length of the String
, in bytes.
fn main() {
let s = String::from("Hello");
println!("Length: {}", s.len());
}
// Output: Length: 5
capacity
Returns the capacity of the String
, in bytes.
fn main() {
let s = String::with_capacity(10);
println!("Capacity: {}", s.capacity());
}
// Output: Capacity: 10
clear
Clears the contents of the String
, making it empty.
fn main() {
let mut s = String::from("Hello");
s.clear();
println!("Cleared string: '{}'", s);
}
// Output: Cleared string: ''
is_empty
Checks if the String
is empty.
fn main() {
let s = String::new();
println!("Is empty: {}", s.is_empty());
}
// Output: Is empty: true
Example Usage
Example 1: Basic Usage
fn main() {
let mut s = String::new();
s.push_str("Hello");
s.push_str(", world!");
println!("{}", s);
}
// Output: Hello, world!
Example 2: Creating a String from a Literal
fn main() {
let s = String::from("Hello, Rust!");
println!("{}", s);
}
// Output: Hello, Rust!
Example 3: Concatenation
fn main() {
let s1 = String::from("Hello, ");
let s2 = String::from("Rust!");
let s3 = s1 + &s2; // Note that s1 has been moved here and can no longer be used
println!("{}", s3);
}
// Output: Hello, Rust!
Example 4: Iterating Over Characters
fn main() {
let s = String::from("Hello");
for c in s.chars() {
println!("{}", c);
}
}
// Output:
// H
// e
// l
// l
// o
Example 5: String Slicing
fn main() {
let s = String::from("Hello, Rust!");
let slice = &s[0..5];
println!("Slice: {}", slice);
}
// Output: Slice: Hello
Considerations
- The
String
type is UTF-8 encoded, meaning it can contain any valid Unicode sequence. - When concatenating strings, consider using the
format!
macro for more complex cases to avoid ownership issues. - String slicing can be dangerous if not done at valid character boundaries; slicing in the middle of a multi-byte character will cause a runtime panic.
See Also
- str - The borrowed string slice type, typically seen in its borrowed form as
&str
. - Vec - A contiguous growable array type with heap-allocated contents, often used for collections of characters.
- Cow - A clone-on-write smart pointer used for situations where you want to work with either a borrowed or owned string.