Skip to content

Commit 98f1b0f

Browse files
committed
auto merge of #16333 : steveklabnik/rust/guide_strings, r=brson
I _think_ this is the right place to introduce strings. It's a bit hard to talk about without understanding pointers and ownership, but you need to have some idea of what's going on...
2 parents ca89cfb + e0fa999 commit 98f1b0f

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/doc/guide.md

+80
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,86 @@ building our guessing game, but we need to know how to do one last thing first:
14321432
get input from the keyboard. You can't have a guessing game without the ability
14331433
to guess!
14341434

1435+
# Strings
1436+
1437+
Strings are an important concept for any programmer to master. Rust's string
1438+
handling system is a bit different than in other languages, due to its systems
1439+
focus. Any time you have a data structure of variable size, things can get
1440+
tricky, and strings are a re-sizable data structure. That said, Rust's strings
1441+
also work differently than in some other systems languages, such as C.
1442+
1443+
Let's dig into the details. A **string** is a sequence of unicode scalar values
1444+
encoded as a stream of UTF-8 bytes. All strings are guaranteed to be
1445+
validly-encoded UTF-8 sequences. Additionally, strings are not null-terminated
1446+
and can contain null bytes.
1447+
1448+
Rust has two main types of strings: `&str` and `String`.
1449+
1450+
The first kind is a `&str`. This is pronounced a 'string slice.' String literals
1451+
are of the type `&str`:
1452+
1453+
```{rust}
1454+
let string = "Hello there.";
1455+
```
1456+
1457+
This string is statically allocated, meaning that it's saved inside our
1458+
compiled program, and exists for the entire duration it runs. The `string`
1459+
binding is a reference to this statically allocated string. String slices
1460+
have a fixed size, and cannot be mutated.
1461+
1462+
A `String`, on the other hand, is an in-memory string. This string is
1463+
growable, and is also guaranteed to be UTF-8.
1464+
1465+
```{rust}
1466+
let mut s = "Hello".to_string();
1467+
println!("{}", s);
1468+
1469+
s.push_str(", world.");
1470+
println!("{}", s);
1471+
```
1472+
1473+
You can coerce a `String` into a `&str` with the `as_slice()` method:
1474+
1475+
```{rust}
1476+
fn takes_slice(slice: &str) {
1477+
println!("Got: {}", slice);
1478+
}
1479+
1480+
fn main() {
1481+
let s = "Hello".to_string();
1482+
takes_slice(s.as_slice());
1483+
}
1484+
```
1485+
1486+
To compare a String to a constant string, prefer `as_slice()`...
1487+
1488+
```{rust}
1489+
fn compare(string: String) {
1490+
if string.as_slice() == "Hello" {
1491+
println!("yes");
1492+
}
1493+
}
1494+
```
1495+
1496+
... over `to_string()`:
1497+
1498+
```{rust}
1499+
fn compare(string: String) {
1500+
if string == "Hello".to_string() {
1501+
println!("yes");
1502+
}
1503+
}
1504+
```
1505+
1506+
Converting a `String` to a `&str` is cheap, but converting the `&str` to a
1507+
`String` involves allocating memory. No reason to do that unless you have to!
1508+
1509+
That's the basics of strings in Rust! They're probably a bit more complicated
1510+
than you are used to, if you come from a scripting language, but when the
1511+
low-level details matter, they really matter. Just remember that `String`s
1512+
allocate memory and control their data, while `&str`s are a reference to
1513+
another string, and you'll be all set.
1514+
14351515
# Standard Input
14361516

14371517
Getting input from the keyboard is pretty easy, but uses some things

0 commit comments

Comments
 (0)