Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clarity to the examples of some Vec & VecDeque methods #134310

Merged
merged 3 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1869,7 +1869,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
///
/// # Panics
///
/// Panics if `index` is greater than deque's length
/// Panics if `index` is strictly greater than deque's length
///
/// # Examples
///
Expand All @@ -1884,6 +1884,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
///
/// vec_deque.insert(1, 'd');
/// assert_eq!(vec_deque, &['a', 'd', 'b', 'c']);
///
/// vec_deque.insert(4, 'e');
/// assert_eq!(vec_deque, &['a', 'd', 'b', 'c', 'e']);
/// ```
#[stable(feature = "deque_extras_15", since = "1.5.0")]
#[track_caller]
Expand Down Expand Up @@ -1928,13 +1931,13 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// use std::collections::VecDeque;
///
/// let mut buf = VecDeque::new();
/// buf.push_back(1);
/// buf.push_back(2);
/// buf.push_back(3);
/// assert_eq!(buf, [1, 2, 3]);
/// buf.push_back('a');
/// buf.push_back('b');
/// buf.push_back('c');
/// assert_eq!(buf, ['a', 'b', 'c']);
///
/// assert_eq!(buf.remove(1), Some(2));
/// assert_eq!(buf, [1, 3]);
/// assert_eq!(buf.remove(1), Some('b'));
/// assert_eq!(buf, ['a', 'c']);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_confusables("delete", "take")]
Expand Down Expand Up @@ -1982,10 +1985,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
/// ```
/// use std::collections::VecDeque;
///
/// let mut buf: VecDeque<_> = [1, 2, 3].into();
/// let mut buf: VecDeque<_> = ['a', 'b', 'c'].into();
/// let buf2 = buf.split_off(1);
/// assert_eq!(buf, [1]);
/// assert_eq!(buf2, [2, 3]);
/// assert_eq!(buf, ['a']);
/// assert_eq!(buf2, ['b', 'c']);
/// ```
#[inline]
#[must_use = "use `.truncate()` if you don't need the other half"]
Expand Down
28 changes: 14 additions & 14 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1953,11 +1953,11 @@ impl<T, A: Allocator> Vec<T, A> {
/// # Examples
///
/// ```
/// let mut vec = vec![1, 2, 3];
/// vec.insert(1, 4);
/// assert_eq!(vec, [1, 4, 2, 3]);
/// vec.insert(4, 5);
/// assert_eq!(vec, [1, 4, 2, 3, 5]);
/// let mut vec = vec!['a', 'b', 'c'];
/// vec.insert(1, 'd');
/// assert_eq!(vec, ['a', 'd', 'b', 'c']);
/// vec.insert(4, 'e');
/// assert_eq!(vec, ['a', 'd', 'b', 'c', 'e']);
/// ```
///
/// # Time complexity
Expand Down Expand Up @@ -2024,9 +2024,9 @@ impl<T, A: Allocator> Vec<T, A> {
/// # Examples
///
/// ```
/// let mut v = vec![1, 2, 3];
/// assert_eq!(v.remove(1), 2);
/// assert_eq!(v, [1, 3]);
/// let mut v = vec!['a', 'b', 'c'];
/// assert_eq!(v.remove(1), 'b');
/// assert_eq!(v, ['a', 'c']);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[track_caller]
Expand Down Expand Up @@ -2715,10 +2715,10 @@ impl<T, A: Allocator> Vec<T, A> {
/// # Examples
///
/// ```
/// let mut vec = vec![1, 2, 3];
/// let mut vec = vec!['a', 'b', 'c'];
/// let vec2 = vec.split_off(1);
/// assert_eq!(vec, [1]);
/// assert_eq!(vec2, [2, 3]);
/// assert_eq!(vec, ['a']);
/// assert_eq!(vec2, ['b', 'c']);
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
Expand Down Expand Up @@ -2982,9 +2982,9 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
/// vec.resize(3, "world");
/// assert_eq!(vec, ["hello", "world", "world"]);
///
/// let mut vec = vec![1, 2, 3, 4];
/// vec.resize(2, 0);
/// assert_eq!(vec, [1, 2]);
/// let mut vec = vec!['a', 'b', 'c', 'd'];
/// vec.resize(2, '_');
/// assert_eq!(vec, ['a', 'b']);
/// ```
#[cfg(not(no_global_oom_handling))]
#[stable(feature = "vec_resize", since = "1.5.0")]
Expand Down
24 changes: 12 additions & 12 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1883,23 +1883,23 @@ impl<T> [T] {
/// # Examples
///
/// ```
/// let v = [1, 2, 3, 4, 5, 6];
/// let v = ['a', 'b', 'c'];
///
/// {
/// let (left, right) = v.split_at(0);
/// assert_eq!(left, []);
/// assert_eq!(right, [1, 2, 3, 4, 5, 6]);
/// assert_eq!(right, ['a', 'b', 'c']);
/// }
///
/// {
/// let (left, right) = v.split_at(2);
/// assert_eq!(left, [1, 2]);
/// assert_eq!(right, [3, 4, 5, 6]);
/// assert_eq!(left, ['a', 'b']);
/// assert_eq!(right, ['c']);
/// }
///
/// {
/// let (left, right) = v.split_at(6);
/// assert_eq!(left, [1, 2, 3, 4, 5, 6]);
/// let (left, right) = v.split_at(3);
/// assert_eq!(left, ['a', 'b', 'c']);
/// assert_eq!(right, []);
/// }
/// ```
Expand Down Expand Up @@ -1969,23 +1969,23 @@ impl<T> [T] {
/// # Examples
///
/// ```
/// let v = [1, 2, 3, 4, 5, 6];
/// let v = ['a', 'b', 'c'];
///
/// unsafe {
/// let (left, right) = v.split_at_unchecked(0);
/// assert_eq!(left, []);
/// assert_eq!(right, [1, 2, 3, 4, 5, 6]);
/// assert_eq!(right, ['a', 'b', 'c']);
/// }
///
/// unsafe {
/// let (left, right) = v.split_at_unchecked(2);
/// assert_eq!(left, [1, 2]);
/// assert_eq!(right, [3, 4, 5, 6]);
/// assert_eq!(left, ['a', 'b']);
/// assert_eq!(right, ['c']);
/// }
///
/// unsafe {
/// let (left, right) = v.split_at_unchecked(6);
/// assert_eq!(left, [1, 2, 3, 4, 5, 6]);
/// let (left, right) = v.split_at_unchecked(3);
/// assert_eq!(left, ['a', 'b', 'c']);
/// assert_eq!(right, []);
/// }
/// ```
Expand Down
Loading