Skip to content

Commit 38917ee

Browse files
committed
Mention the lack of windows_mut in windows
1 parent bfffe40 commit 38917ee

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

library/core/src/slice/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,22 @@ impl<T> [T] {
781781
/// let mut iter = slice.windows(4);
782782
/// assert!(iter.next().is_none());
783783
/// ```
784+
///
785+
/// There's no `windows_mut`, as that existing would let safe code violate the
786+
/// "only one `&mut` at a time to the same thing" rule. However, you can sometimes
787+
/// use [`Cell::as_slice_of_cells`](crate::cell::Cell::as_slice_of_cells) in
788+
/// conjunction with `windows` to accomplish something similar:
789+
/// ```
790+
/// use std::cell::Cell;
791+
///
792+
/// let mut array = ['R', 'u', 's', 't', ' ', '2', '0', '1', '5'];
793+
/// let slice = &mut array[..];
794+
/// let slice_of_cells: &[Cell<char>] = Cell::from_mut(slice).as_slice_of_cells();
795+
/// for w in slice_of_cells.windows(3) {
796+
/// Cell::swap(&w[0], &w[2]);
797+
/// }
798+
/// assert_eq!(array, ['s', 't', ' ', '2', '0', '1', '5', 'u', 'R']);
799+
/// ```
784800
#[stable(feature = "rust1", since = "1.0.0")]
785801
#[inline]
786802
pub fn windows(&self, size: usize) -> Windows<'_, T> {

0 commit comments

Comments
 (0)