@@ -24,21 +24,45 @@ declare_clippy_lint! {
24
24
/// ### Example
25
25
///
26
26
/// ```no_run
27
+ /// #![feature(vec_into_raw_parts)]
27
28
/// let mut original: Vec::<i32> = Vec::with_capacity(20);
28
29
/// original.extend([1, 2, 3, 4, 5]);
29
30
///
30
31
/// let (ptr, mut len, cap) = original.into_raw_parts();
31
32
///
32
- /// // Pretend we added three more integers:
33
- /// len = 8;
33
+ /// // I will add three more integers:
34
+ /// unsafe {
35
+ /// let ptr = ptr as *mut i32;
36
+ ///
37
+ /// for i in 6..9 {
38
+ /// *ptr.add(i - 1) = i as i32;
39
+ /// len += 1;
40
+ /// }
41
+ /// }
34
42
///
35
43
/// // But I forgot the capacity was separate from the length:
36
44
/// let reconstructed = unsafe { Vec::from_raw_parts(ptr, len, len) };
37
45
/// ```
38
46
///
39
47
/// Use instead:
40
48
/// ```no_run
41
- /// // Correction to the last line of the given example code:
49
+ /// #![feature(vec_into_raw_parts)]
50
+ /// let mut original: Vec::<i32> = Vec::with_capacity(20);
51
+ /// original.extend([1, 2, 3, 4, 5]);
52
+ ///
53
+ /// let (ptr, mut len, cap) = original.into_raw_parts();
54
+ ///
55
+ /// // I will add three more integers:
56
+ /// unsafe {
57
+ /// let ptr = ptr as *mut i32;
58
+ ///
59
+ /// for i in 6..9 {
60
+ /// *ptr.add(i - 1) = i as i32;
61
+ /// len += 1;
62
+ /// }
63
+ /// }
64
+ ///
65
+ /// // This time, leverage the previously saved capacity:
42
66
/// let reconstructed = unsafe { Vec::from_raw_parts(ptr, len, cap) };
43
67
/// ```
44
68
#[ clippy:: version = "1.91.0" ]
0 commit comments