Skip to content

Commit b974187

Browse files
authored
Rollup merge of #134310 - tkr-sh:master, r=Noratrieb
Add clarity to the examples of some `Vec` & `VecDeque` methods In some `Vec` and `VecDeque` examples where elements are `i32`, examples can seem a bit confusing at first glance if a parameter of the method is an `usize`. In this case, I think it's better to use `char` rather than `i32`. > [!NOTE] > It's already done in the implementation of `VecDeque::insert` #### Difference - `i32` ```rs let mut v = vec![1, 2, 3]; assert_eq!(v.remove(1), 2); assert_eq!(v, [1, 3]); ``` - `char` ```rs let mut v = vec!['a', 'b', 'c']; assert_eq!(v.remove(1), 'b'); assert_eq!(v, ['a', 'c']); ``` Even tho it's pretty minor, it's a nice to have.
2 parents d48af09 + 6d5c591 commit b974187

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

library/alloc/src/collections/vec_deque/mod.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
18691869
///
18701870
/// # Panics
18711871
///
1872-
/// Panics if `index` is greater than deque's length
1872+
/// Panics if `index` is strictly greater than deque's length
18731873
///
18741874
/// # Examples
18751875
///
@@ -1884,6 +1884,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
18841884
///
18851885
/// vec_deque.insert(1, 'd');
18861886
/// assert_eq!(vec_deque, &['a', 'd', 'b', 'c']);
1887+
///
1888+
/// vec_deque.insert(4, 'e');
1889+
/// assert_eq!(vec_deque, &['a', 'd', 'b', 'c', 'e']);
18871890
/// ```
18881891
#[stable(feature = "deque_extras_15", since = "1.5.0")]
18891892
#[track_caller]
@@ -1928,13 +1931,13 @@ impl<T, A: Allocator> VecDeque<T, A> {
19281931
/// use std::collections::VecDeque;
19291932
///
19301933
/// let mut buf = VecDeque::new();
1931-
/// buf.push_back(1);
1932-
/// buf.push_back(2);
1933-
/// buf.push_back(3);
1934-
/// assert_eq!(buf, [1, 2, 3]);
1934+
/// buf.push_back('a');
1935+
/// buf.push_back('b');
1936+
/// buf.push_back('c');
1937+
/// assert_eq!(buf, ['a', 'b', 'c']);
19351938
///
1936-
/// assert_eq!(buf.remove(1), Some(2));
1937-
/// assert_eq!(buf, [1, 3]);
1939+
/// assert_eq!(buf.remove(1), Some('b'));
1940+
/// assert_eq!(buf, ['a', 'c']);
19381941
/// ```
19391942
#[stable(feature = "rust1", since = "1.0.0")]
19401943
#[rustc_confusables("delete", "take")]
@@ -1982,10 +1985,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
19821985
/// ```
19831986
/// use std::collections::VecDeque;
19841987
///
1985-
/// let mut buf: VecDeque<_> = [1, 2, 3].into();
1988+
/// let mut buf: VecDeque<_> = ['a', 'b', 'c'].into();
19861989
/// let buf2 = buf.split_off(1);
1987-
/// assert_eq!(buf, [1]);
1988-
/// assert_eq!(buf2, [2, 3]);
1990+
/// assert_eq!(buf, ['a']);
1991+
/// assert_eq!(buf2, ['b', 'c']);
19891992
/// ```
19901993
#[inline]
19911994
#[must_use = "use `.truncate()` if you don't need the other half"]

library/alloc/src/vec/mod.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -1953,11 +1953,11 @@ impl<T, A: Allocator> Vec<T, A> {
19531953
/// # Examples
19541954
///
19551955
/// ```
1956-
/// let mut vec = vec![1, 2, 3];
1957-
/// vec.insert(1, 4);
1958-
/// assert_eq!(vec, [1, 4, 2, 3]);
1959-
/// vec.insert(4, 5);
1960-
/// assert_eq!(vec, [1, 4, 2, 3, 5]);
1956+
/// let mut vec = vec!['a', 'b', 'c'];
1957+
/// vec.insert(1, 'd');
1958+
/// assert_eq!(vec, ['a', 'd', 'b', 'c']);
1959+
/// vec.insert(4, 'e');
1960+
/// assert_eq!(vec, ['a', 'd', 'b', 'c', 'e']);
19611961
/// ```
19621962
///
19631963
/// # Time complexity
@@ -2024,9 +2024,9 @@ impl<T, A: Allocator> Vec<T, A> {
20242024
/// # Examples
20252025
///
20262026
/// ```
2027-
/// let mut v = vec![1, 2, 3];
2028-
/// assert_eq!(v.remove(1), 2);
2029-
/// assert_eq!(v, [1, 3]);
2027+
/// let mut v = vec!['a', 'b', 'c'];
2028+
/// assert_eq!(v.remove(1), 'b');
2029+
/// assert_eq!(v, ['a', 'c']);
20302030
/// ```
20312031
#[stable(feature = "rust1", since = "1.0.0")]
20322032
#[track_caller]
@@ -2715,10 +2715,10 @@ impl<T, A: Allocator> Vec<T, A> {
27152715
/// # Examples
27162716
///
27172717
/// ```
2718-
/// let mut vec = vec![1, 2, 3];
2718+
/// let mut vec = vec!['a', 'b', 'c'];
27192719
/// let vec2 = vec.split_off(1);
2720-
/// assert_eq!(vec, [1]);
2721-
/// assert_eq!(vec2, [2, 3]);
2720+
/// assert_eq!(vec, ['a']);
2721+
/// assert_eq!(vec2, ['b', 'c']);
27222722
/// ```
27232723
#[cfg(not(no_global_oom_handling))]
27242724
#[inline]
@@ -2982,9 +2982,9 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
29822982
/// vec.resize(3, "world");
29832983
/// assert_eq!(vec, ["hello", "world", "world"]);
29842984
///
2985-
/// let mut vec = vec![1, 2, 3, 4];
2986-
/// vec.resize(2, 0);
2987-
/// assert_eq!(vec, [1, 2]);
2985+
/// let mut vec = vec!['a', 'b', 'c', 'd'];
2986+
/// vec.resize(2, '_');
2987+
/// assert_eq!(vec, ['a', 'b']);
29882988
/// ```
29892989
#[cfg(not(no_global_oom_handling))]
29902990
#[stable(feature = "vec_resize", since = "1.5.0")]

library/core/src/slice/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1883,23 +1883,23 @@ impl<T> [T] {
18831883
/// # Examples
18841884
///
18851885
/// ```
1886-
/// let v = [1, 2, 3, 4, 5, 6];
1886+
/// let v = ['a', 'b', 'c'];
18871887
///
18881888
/// {
18891889
/// let (left, right) = v.split_at(0);
18901890
/// assert_eq!(left, []);
1891-
/// assert_eq!(right, [1, 2, 3, 4, 5, 6]);
1891+
/// assert_eq!(right, ['a', 'b', 'c']);
18921892
/// }
18931893
///
18941894
/// {
18951895
/// let (left, right) = v.split_at(2);
1896-
/// assert_eq!(left, [1, 2]);
1897-
/// assert_eq!(right, [3, 4, 5, 6]);
1896+
/// assert_eq!(left, ['a', 'b']);
1897+
/// assert_eq!(right, ['c']);
18981898
/// }
18991899
///
19001900
/// {
1901-
/// let (left, right) = v.split_at(6);
1902-
/// assert_eq!(left, [1, 2, 3, 4, 5, 6]);
1901+
/// let (left, right) = v.split_at(3);
1902+
/// assert_eq!(left, ['a', 'b', 'c']);
19031903
/// assert_eq!(right, []);
19041904
/// }
19051905
/// ```
@@ -1969,23 +1969,23 @@ impl<T> [T] {
19691969
/// # Examples
19701970
///
19711971
/// ```
1972-
/// let v = [1, 2, 3, 4, 5, 6];
1972+
/// let v = ['a', 'b', 'c'];
19731973
///
19741974
/// unsafe {
19751975
/// let (left, right) = v.split_at_unchecked(0);
19761976
/// assert_eq!(left, []);
1977-
/// assert_eq!(right, [1, 2, 3, 4, 5, 6]);
1977+
/// assert_eq!(right, ['a', 'b', 'c']);
19781978
/// }
19791979
///
19801980
/// unsafe {
19811981
/// let (left, right) = v.split_at_unchecked(2);
1982-
/// assert_eq!(left, [1, 2]);
1983-
/// assert_eq!(right, [3, 4, 5, 6]);
1982+
/// assert_eq!(left, ['a', 'b']);
1983+
/// assert_eq!(right, ['c']);
19841984
/// }
19851985
///
19861986
/// unsafe {
1987-
/// let (left, right) = v.split_at_unchecked(6);
1988-
/// assert_eq!(left, [1, 2, 3, 4, 5, 6]);
1987+
/// let (left, right) = v.split_at_unchecked(3);
1988+
/// assert_eq!(left, ['a', 'b', 'c']);
19891989
/// assert_eq!(right, []);
19901990
/// }
19911991
/// ```

0 commit comments

Comments
 (0)