Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7b30f5c

Browse files
committedNov 30, 2015
Better docs for the str primitive
Part of #29338
1 parent edf2198 commit 7b30f5c

File tree

3 files changed

+622
-257
lines changed

3 files changed

+622
-257
lines changed
 

‎src/libcollections/str.rs

Lines changed: 579 additions & 234 deletions
Large diffs are not rendered by default.

‎src/libstd/primitive_docs.rs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -293,44 +293,64 @@ mod prim_slice { }
293293

294294
#[doc(primitive = "str")]
295295
//
296-
/// Unicode string slices.
296+
/// String slices.
297297
///
298-
/// Rust's `str` type is one of the core primitive types of the language. `&str`
299-
/// is the borrowed string type. This type of string can only be created from
300-
/// other strings, unless it is a `&'static str` (see below). It is not possible
301-
/// to move out of borrowed strings because they are owned elsewhere.
298+
/// The `str` type, also called a 'string slice', is the most primitive string
299+
/// type. It is usually seen in its borrowed form, `&str`. It is also the type
300+
/// of string literals, `&'static str`.
301+
///
302+
/// Strings slices are always valid UTF-8.
303+
///
304+
/// This documentation describes a number of methods and trait implementations
305+
/// on the `str` type. For technical reasons, there is additional, separate
306+
/// documentation in [the `std::str` module](str/index.html) as well.
302307
///
303308
/// # Examples
304309
///
305-
/// Here's some code that uses a `&str`:
310+
/// String literals are string slices:
306311
///
307312
/// ```
308-
/// let s = "Hello, world.";
313+
/// let hello = "Hello, world!";
314+
///
315+
/// // with an explicit type annotation
316+
/// let hello: &'static str = "Hello, world!";
309317
/// ```
310318
///
311-
/// This `&str` is a `&'static str`, which is the type of string literals.
312-
/// They're `'static` because literals are available for the entire lifetime of
313-
/// the program.
319+
/// They are `'static` because they're stored directly in the final binary, and
320+
/// so will be valid for the `'static` duration.
314321
///
315-
/// You can get a non-`'static` `&str` by taking a slice of a `String`:
322+
/// # Representation
323+
///
324+
/// A `&str` is made up of two components: a pointer to some bytes, and a
325+
/// length. You can look at these with the [`.as_ptr()`] and [`len()`] methods:
316326
///
317327
/// ```
318-
/// let some_string = "Hello, world.".to_string();
319-
/// let s = &some_string;
320-
/// ```
328+
/// use std::slice;
329+
/// use std::str;
321330
///
322-
/// # Representation
331+
/// let story = "Once upon a time...";
332+
///
333+
/// let ptr = story.as_ptr();
334+
/// let len = story.len();
323335
///
324-
/// Rust's string type, `str`, is a sequence of Unicode scalar values encoded as
325-
/// a stream of UTF-8 bytes. All [strings](../../reference.html#literals) are
326-
/// guaranteed to be validly encoded UTF-8 sequences. Additionally, strings are
327-
/// not null-terminated and can thus contain null bytes.
336+
/// // story has thirteen bytes
337+
/// assert_eq!(19, len);
328338
///
329-
/// The actual representation of `str`s have direct mappings to slices: `&str`
330-
/// is the same as `&[u8]`.
339+
/// // We can re-build a str out of ptr and len. This is all unsafe becuase
340+
/// // we are responsible for making sure the two components are valid:
341+
/// let s = unsafe {
342+
/// // First, we build a &[u8]...
343+
/// let slice = slice::from_raw_parts(ptr, len);
331344
///
332-
/// *[See also the `std::str` module](str/index.html).*
345+
/// // ... and then convert that slice into a string slice
346+
/// str::from_utf8(slice)
347+
/// };
348+
///
349+
/// assert_eq!(s, Ok(story));
350+
/// ```
333351
///
352+
/// [`.as_ptr()`]: #method.as_ptr
353+
/// [`len()`]: # method.len
334354
mod prim_str { }
335355

336356
#[doc(primitive = "tuple")]

0 commit comments

Comments
 (0)
Please sign in to comment.