Skip to content

Commit e6c83dd

Browse files
committed
Document that slice means pointer to a sequence
Also document that slices are twice as large as pointers to Sized types
1 parent cfdf9d3 commit e6c83dd

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

library/std/src/primitive_docs.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,8 @@ mod prim_array {}
565565
///
566566
/// *[See also the `std::slice` module](slice/index.html).*
567567
///
568-
/// Slices are a view into a block of memory represented as a pointer and a
569-
/// length.
568+
/// A slice is any pointer/reference to a block of memory. They are represented
569+
/// as a regular pointer and a length.
570570
///
571571
/// ```
572572
/// // slicing a Vec
@@ -587,6 +587,19 @@ mod prim_array {}
587587
/// x[1] = 7;
588588
/// assert_eq!(x, &[1, 7, 3]);
589589
/// ```
590+
///
591+
/// As slices store the length of the sequence they refer to, they have twice
592+
/// the size of pointers to [`Sized`](marker/trait.Sized.html) types.
593+
/// Also see the reference on
594+
/// [dynamically sized types](../reference/dynamically-sized-types.html)
595+
///
596+
/// ```
597+
/// let pointer_size = std::mem::size_of::<&u8>();
598+
/// assert_eq!(2 * pointer_size, std::mem::size_of::<&[u8]>());
599+
/// assert_eq!(2 * pointer_size, std::mem::size_of::<*const [u8]>());
600+
/// assert_eq!(2 * pointer_size, std::mem::size_of::<Box<[u8]>>());
601+
/// assert_eq!(2 * pointer_size, std::mem::size_of::<Rc<[u8]>>());
602+
/// ```
590603
#[stable(feature = "rust1", since = "1.0.0")]
591604
mod prim_slice {}
592605

0 commit comments

Comments
 (0)