@@ -293,44 +293,64 @@ mod prim_slice { }
293
293
294
294
#[ doc( primitive = "str" ) ]
295
295
//
296
- /// Unicode string slices.
296
+ /// String slices.
297
297
///
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.
302
307
///
303
308
/// # Examples
304
309
///
305
- /// Here's some code that uses a `&str` :
310
+ /// String literals are string slices :
306
311
///
307
312
/// ```
308
- /// let s = "Hello, world.";
313
+ /// let hello = "Hello, world!";
314
+ ///
315
+ /// // with an explicit type annotation
316
+ /// let hello: &'static str = "Hello, world!";
309
317
/// ```
310
318
///
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.
314
321
///
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:
316
326
///
317
327
/// ```
318
- /// let some_string = "Hello, world.".to_string();
319
- /// let s = &some_string;
320
- /// ```
328
+ /// use std::slice;
329
+ /// use std::str;
321
330
///
322
- /// # Representation
331
+ /// let story = "Once upon a time...";
332
+ ///
333
+ /// let ptr = story.as_ptr();
334
+ /// let len = story.len();
323
335
///
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);
328
338
///
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);
331
344
///
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
+ /// ```
333
351
///
352
+ /// [`.as_ptr()`]: #method.as_ptr
353
+ /// [`len()`]: # method.len
334
354
mod prim_str { }
335
355
336
356
#[ doc( primitive = "tuple" ) ]
0 commit comments