about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-12-01 03:37:58 +0000
committerbors <bors@rust-lang.org>2015-12-01 03:37:58 +0000
commitbaf020802fcc8fd4f8b1890932dde3d2a5a37596 (patch)
tree475d325d36038a1bf585113a6458c4b6bcfc951a /src/libstd
parent407e8b3aacfd3a31dff7fdf66fa381fa5064b237 (diff)
parent7b30f5c2563a27e45593aa72b7f34ee49f62144f (diff)
downloadrust-baf020802fcc8fd4f8b1890932dde3d2a5a37596.tar.gz
rust-baf020802fcc8fd4f8b1890932dde3d2a5a37596.zip
Auto merge of #30057 - steveklabnik:doc_str, r=alexcrichton
Part of #29338
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/primitive_docs.rs64
1 files changed, 42 insertions, 22 deletions
diff --git a/src/libstd/primitive_docs.rs b/src/libstd/primitive_docs.rs
index aec8b6b1b22..e0d2011b293 100644
--- a/src/libstd/primitive_docs.rs
+++ b/src/libstd/primitive_docs.rs
@@ -293,44 +293,64 @@ mod prim_slice { }
 
 #[doc(primitive = "str")]
 //
-/// Unicode string slices.
+/// String slices.
 ///
-/// Rust's `str` type is one of the core primitive types of the language. `&str`
-/// is the borrowed string type. This type of string can only be created from
-/// other strings, unless it is a `&'static str` (see below). It is not possible
-/// to move out of borrowed strings because they are owned elsewhere.
+/// The `str` type, also called a 'string slice', is the most primitive string
+/// type. It is usually seen in its borrowed form, `&str`. It is also the type
+/// of string literals, `&'static str`.
+///
+/// Strings slices are always valid UTF-8.
+///
+/// This documentation describes a number of methods and trait implementations
+/// on the `str` type. For technical reasons, there is additional, separate
+/// documentation in [the `std::str` module](str/index.html) as well.
 ///
 /// # Examples
 ///
-/// Here's some code that uses a `&str`:
+/// String literals are string slices:
 ///
 /// ```
-/// let s = "Hello, world.";
+/// let hello = "Hello, world!";
+///
+/// // with an explicit type annotation
+/// let hello: &'static str = "Hello, world!";
 /// ```
 ///
-/// This `&str` is a `&'static str`, which is the type of string literals.
-/// They're `'static` because literals are available for the entire lifetime of
-/// the program.
+/// They are `'static` because they're stored directly in the final binary, and
+/// so will be valid for the `'static` duration.
 ///
-/// You can get a non-`'static` `&str` by taking a slice of a `String`:
+/// # Representation
+///
+/// A `&str` is made up of two components: a pointer to some bytes, and a
+/// length. You can look at these with the [`.as_ptr()`] and [`len()`] methods:
 ///
 /// ```
-/// let some_string = "Hello, world.".to_string();
-/// let s = &some_string;
-/// ```
+/// use std::slice;
+/// use std::str;
 ///
-/// # Representation
+/// let story = "Once upon a time...";
+///
+/// let ptr = story.as_ptr();
+/// let len = story.len();
 ///
-/// Rust's string type, `str`, is a sequence of Unicode scalar values encoded as
-/// a stream of UTF-8 bytes. All [strings](../../reference.html#literals) are
-/// guaranteed to be validly encoded UTF-8 sequences. Additionally, strings are
-/// not null-terminated and can thus contain null bytes.
+/// // story has thirteen bytes
+/// assert_eq!(19, len);
 ///
-/// The actual representation of `str`s have direct mappings to slices: `&str`
-/// is the same as `&[u8]`.
+/// // We can re-build a str out of ptr and len. This is all unsafe becuase
+/// // we are responsible for making sure the two components are valid:
+/// let s = unsafe {
+///     // First, we build a &[u8]...
+///     let slice = slice::from_raw_parts(ptr, len);
 ///
-/// *[See also the `std::str` module](str/index.html).*
+///     // ... and then convert that slice into a string slice
+///     str::from_utf8(slice)
+/// };
+///
+/// assert_eq!(s, Ok(story));
+/// ```
 ///
+/// [`.as_ptr()`]: #method.as_ptr
+/// [`len()`]: # method.len
 mod prim_str { }
 
 #[doc(primitive = "tuple")]