3.7 KiB
String
String s are implemented as a collection bytes plus some methods o provide useful functionality when those bytes are interpreted as text.
Strings can do everything that a UTF-8 character can
These attached methods include creating, updating and reading.
Strings are different to other collections, namely how indexing into a String
is complicated by the differences between how people and computers interpret String
data
What is a String?
There is only one string type in the core language which is the string slice str
this is usually in the borrowed form &str
.
This is special when not referenced because it is a constant written into the binary. This is only the case for string literals
The String type is provided by Rust's standard library rather than coded into the core language is growable, mutable, owned and UTF-8 encoded string type.
When a string is referred to in rust they refer either to the &str
(string slice) or the String
that is included in the std library
string slices are also UTF-9 encoded
Creating a New String
Many of the same operations available with Vec<T>
are available with String
as well because it is implemented as a wrapper around a vector of bytes with some guarantees, restrictions and capabilities.
To create one it works the same as a Vec<T>
let mut s = String::new();
This creates a new empty string, which can then load data
Often times we have some initial data for that we can use the to_string
method which is available on any type that implements the Display
trait, as string literals do
let data = "initial contents";
let s = data.to_string();
// the same as the two lines above
let s = "inital contents".to_string();
These both create a string containing intial contents
You can also use the String::from
to create a string form a string literal
let s = String::form("initial contents");
Because strings are used for so many things we can use many different generic APIs for strings, providing us with a lot of options.
Whilst some can seem redundant but they all have their place.
for String::from
and to_string
whilst they do the same thing which one you choose is a matter of style and readability
UTF-8 Strings, because it has this property it can do any language where all of them are valid
let hello = String::from("السلام عليكم");
let hello = String::from("Dobrý den");
let hello = String::from("Hello");
let hello = String::from("שלום");
let hello = String::from("नमस्ते");
let hello = String::from("こんにちは");
let hello = String::from("안녕하세요");
let hello = String::from("你好");
let hello = String::from("Olá");
let hello = String::from("Здравствуйте");
let hello = String::from("Hola");
Updating a String
A String
can grow in size and its contents can change, just like the contents of a Vec<T>
if you push more data into it.
In addition you can use the +
operator or the format!
macro to concatenate String
values.
Appending to a String with push_str and push
Strings can be grown by using the push_str
method to append to a string slice
let mut s = String::from("foo");
s.push_str("bar");
after the two lines the string will contain foobar
The push_str
takes a string slice because it doesn't necessarily want to take ownership of the parameter, therefore we are able to use a part we borrowed for appending
The push
method takes a single character as a parameter and adds it to the String
let mut s = String::from("lo");
s.push('l');