3 Strings are an important concept for any programmer to master. Rust’s string
4 handling system is a bit different from other languages, due to its systems
5 focus. Any time you have a data structure of variable size, things can get
6 tricky, and strings are a re-sizable data structure. That being said, Rust’s
7 strings also work differently than in some other systems languages, such as C.
9 Let’s dig into the details. A ‘string’ is a sequence of Unicode scalar values
10 encoded as a stream of UTF-8 bytes. All strings are guaranteed to be a valid
11 encoding of UTF-8 sequences. Additionally, unlike some systems languages,
12 strings are not null-terminated and can contain null bytes.
14 Rust has two main types of strings: `&str` and `String`. Let’s talk about
15 `&str` first. These are called ‘string slices’. A string slice has a fixed
16 size, and cannot be mutated. It is a reference to a sequence of UTF-8 bytes.
19 let greeting = "Hello there."; // greeting: &'static str
22 `"Hello there."` is a string literal and its type is `&'static str`. A string
23 literal is a string slice that is statically allocated, meaning that it’s saved
24 inside our compiled program, and exists for the entire duration it runs. The
25 `greeting` binding is a reference to this statically allocated string. Any
26 function expecting a string slice will also accept a string literal.
28 String literals can span multiple lines. There are two forms. The first will
29 include the newline and the leading spaces:
35 assert_eq!("foo\n bar", s);
38 The second, with a `\`, trims the spaces and the newline:
44 assert_eq!("foobar", s);
47 Rust has more than only `&str`s though. A `String`, is a heap-allocated string.
48 This string is growable, and is also guaranteed to be UTF-8. `String`s are
49 commonly created by converting from a string slice using the `to_string`
53 let mut s = "Hello".to_string(); // mut s: String
56 s.push_str(", world.");
60 `String`s will coerce into `&str` with an `&`:
63 fn takes_slice(slice: &str) {
64 println!("Got: {}", slice);
68 let s = "Hello".to_string();
73 This coercion does not happen for functions that accept one of `&str`’s traits
74 instead of `&str`. For example, [`TcpStream::connect`][connect] has a parameter
75 of type `ToSocketAddrs`. A `&str` is okay but a `String` must be explicitly
79 use std::net::TcpStream;
81 TcpStream::connect("192.168.0.1:3000"); // &str parameter
83 let addr_string = "192.168.0.1:3000".to_string();
84 TcpStream::connect(&*addr_string); // convert addr_string to &str
87 Viewing a `String` as a `&str` is cheap, but converting the `&str` to a
88 `String` involves allocating memory. No reason to do that unless you have to!
92 Because strings are valid UTF-8, strings do not support indexing:
97 println!("The first letter of s is {}", s[0]); // ERROR!!!
100 Usually, access to a vector with `[]` is very fast. But, because each character
101 in a UTF-8 encoded string can be multiple bytes, you have to walk over the
102 string to find the nᵗʰ letter of a string. This is a significantly more
103 expensive operation, and we don’t want to be misleading. Furthermore, ‘letter’
104 isn’t something defined in Unicode, exactly. We can choose to look at a string as
105 individual bytes, or as codepoints:
108 let hachiko = "忠犬ハチ公";
110 for b in hachiko.as_bytes() {
116 for c in hachiko.chars() {
126 229, 191, 160, 231, 138, 172, 227, 131, 143, 227, 131, 129, 229, 133, 172,
130 As you can see, there are more bytes than `char`s.
132 You can get something similar to an index like this:
135 # let hachiko = "忠犬ハチ公";
136 let dog = hachiko.chars().nth(1); // kinda like hachiko[1]
139 This emphasizes that we have to walk from the beginning of the list of `chars`.
143 You can get a slice of a string with slicing syntax:
147 let hachi = &dog[0..5];
150 But note that these are _byte_ offsets, not _character_ offsets. So
151 this will fail at runtime:
155 let hachi = &dog[0..2];
161 thread '<main>' panicked at 'index 0 and/or 2 in `忠犬ハチ公` do not lie on
167 If you have a `String`, you can concatenate a `&str` to the end of it:
170 let hello = "Hello ".to_string();
171 let world = "world!";
173 let hello_world = hello + world;
176 But if you have two `String`s, you need an `&`:
179 let hello = "Hello ".to_string();
180 let world = "world!".to_string();
182 let hello_world = hello + &world;
185 This is because `&String` can automatically coerce to a `&str`. This is a
186 feature called ‘[`Deref` coercions][dc]’.
188 [dc]: deref-coercions.html
189 [connect]: ../std/net/struct.TcpStream.html#method.connect