Skip to content

Commit

Permalink
Guide: strings
Browse files Browse the repository at this point in the history
  • Loading branch information
steveklabnik committed Jul 18, 2014
1 parent e288fc6 commit 226b7d1
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 4 deletions.
129 changes: 129 additions & 0 deletions src/doc/guide-strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
% The Strings Guide

# Strings

Strings are an important concept to master in any programming language. If you
come from a managed language background, you may be surprised at the complexity
of string handling in a systems programming language. Efficient access and
allocation of memory for a dynamically sized structure involves a lot of
details. Luckily, Rust has lots of tools to help us here.

A **string** is a sequence of unicode scalar values encoded as a stream of
UTF-8 bytes. All strings are guaranteed to be validly-encoded UTF-8 sequences.
Additionally, strings are not null-terminated and can contain null bytes.

Rust has two main types of strings: `&str` and `String`.

## &str

The first kind is a `&str`. This is pronounced a 'string slice.' String literals

This comment has been minimized.

Copy link
@kud1ing

kud1ing Sep 12, 2014

Currently it reads to me: There is A and B. The first is A. which bears some obviousness/redundancy. Does that make sense?
Maybe make it The first kind &str is pronounced a 'string slice.?

are of the type `&str`:

```{rust}
let string = "Hello there.";
```

Like any Rust type, string slices have an associated lifetime. A string literal
is a `&'static str`. A string slice can be written without an explicit
lifetime in many cases, such as in function arguments. In these cases the
lifetime will be inferred:

```{rust}
fn takes_slice(slice: &str) {
println!("Got: {}", slice);
}
```

Like vector slices, string slices are simply a pointer plus a length. This
means that they're a 'view' into an already-allocated string, such as a
`&'static str` or a `String`.

## String

A `String` is a heap-allocated string. This string is growable, and is also
guaranteed to be UTF-8.

```{rust}
let mut s = "Hello".to_string();
println!("{}", s);
s.push_str(", world.");
println!("{}", s);
```

You can coerce a `String` into a `&str` with the `as_slice()` method:

```{rust}
fn takes_slice(slice: &str) {
println!("Got: {}", slice);
}
fn main() {
let s = "Hello".to_string();
takes_slice(s.as_slice());
}
```

You can also get a `&str` from a stack-allocated array of bytes:

```{rust}
use std::str;
let x: &[u8] = &[b'a', b'b'];
let stack_str: &str = str::from_utf8(x).unwrap();
```

## Best Practices

### `String` vs. `&str`

In general, you should prefer `String` when you need ownership, and `&str` when
you just need to borrow a string. This is very similar to using `Vec<T>` vs. `&[T]`,
and `T` vs `&T` in general.

This means starting off with this:

```{rust,ignore}
fn foo(s: &str) {
```

and only moving to this:

```{rust,ignore}
fn foo(s: String) {
```

If you have good reason. It's not polite to hold on to ownership you don't
need, and it can make your lifetimes more complex. Furthermore, you can pass
either kind of string into `foo` by using `.as_slice()` on any `String` you
need to pass in, so the `&str` version is more flexible.

This comment has been minimized.

Copy link
@cburgdorf

cburgdorf Jul 18, 2014

Contributor

The last argument doesn't seem very strong to me. If foo took a String rather than a &str you could also feed it either a String or a &str if you would just call to_string on your &str. I think the argument only makes sense in combination with the next paragraph that describes that to_string is more expensive because it involves a new allocation. @steveklabnik , am I wrong?


### Comparisons

To compare a String to a constant string, prefer `as_slice()`...

```{rust}
fn compare(string: String) {
if string.as_slice() == "Hello" {
println!("yes");
}
}
```

... over `to_string()`:

```{rust}
fn compare(string: String) {
if string == "Hello".to_string() {
println!("yes");
}
}
```

Converting a `String` to a `&str` is cheap, but converting the `&str` to a
`String` involves an allocation.

This comment has been minimized.

Copy link
@cburgdorf

cburgdorf Jul 18, 2014

Contributor

I think here is still room for more guidance. For instance if you design a Person struct you would probably design it as:

struct Person {
    firstname: String,
    lastname:  String,
}

You probably want ownership in this case, no?


## Other Documentation

* [the `&str` API documentation](/std/str/index.html)
* [the `String` API documentation](std/string/index.html)
8 changes: 4 additions & 4 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ other languages.
# Representation
Rust's string type, `str`, is a sequence of unicode codepoints encoded as a
stream of UTF-8 bytes. All safely-created strings are guaranteed to be validly
encoded UTF-8 sequences. Additionally, strings are not null-terminated
and can contain null codepoints.
Rust's string type, `str`, is a sequence of unicode scalar values encoded as a
stream of UTF-8 bytes. All strings are guaranteed to be validly encoded UTF-8
sequences. Additionally, strings are not null-terminated and can contain null
bytes.
The actual representation of strings have direct mappings to vectors: `&str`
is the same as `&[u8]`.
Expand Down

5 comments on commit 226b7d1

@bors
Copy link
Contributor

@bors bors commented on 226b7d1 Jul 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from kballard
at steveklabnik@226b7d1

@bors
Copy link
Contributor

@bors bors commented on 226b7d1 Jul 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging steveklabnik/rust/string_guide = 226b7d1 into auto

@bors
Copy link
Contributor

@bors bors commented on 226b7d1 Jul 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

steveklabnik/rust/string_guide = 226b7d1 merged ok, testing candidate = cebed8a

@bors
Copy link
Contributor

@bors bors commented on 226b7d1 Jul 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = cebed8a

Please sign in to comment.