Skip to content

Commit 2a3766e

Browse files
authored
Rollup merge of #113147 - lizhanhui:fix_vec_from_raw_parts_doc_example, r=Mark-Simulacrum
Fix document examples of Vec::from_raw_parts and Vec::from_raw_parts_in These two examples are misplaced.
2 parents be6e38c + 9a67df2 commit 2a3766e

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

library/alloc/src/vec/mod.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -560,22 +560,20 @@ impl<T> Vec<T> {
560560
/// Using memory that was allocated elsewhere:
561561
///
562562
/// ```rust
563-
/// #![feature(allocator_api)]
564-
///
565-
/// use std::alloc::{AllocError, Allocator, Global, Layout};
563+
/// use std::alloc::{alloc, Layout};
566564
///
567565
/// fn main() {
568566
/// let layout = Layout::array::<u32>(16).expect("overflow cannot happen");
569567
///
570568
/// let vec = unsafe {
571-
/// let mem = match Global.allocate(layout) {
572-
/// Ok(mem) => mem.cast::<u32>().as_ptr(),
573-
/// Err(AllocError) => return,
574-
/// };
569+
/// let mem = alloc(layout).cast::<u32>();
570+
/// if mem.is_null() {
571+
/// return;
572+
/// }
575573
///
576574
/// mem.write(1_000_000);
577575
///
578-
/// Vec::from_raw_parts_in(mem, 1, 16, Global)
576+
/// Vec::from_raw_parts(mem, 1, 16)
579577
/// };
580578
///
581579
/// assert_eq!(vec, &[1_000_000]);
@@ -758,19 +756,22 @@ impl<T, A: Allocator> Vec<T, A> {
758756
/// Using memory that was allocated elsewhere:
759757
///
760758
/// ```rust
761-
/// use std::alloc::{alloc, Layout};
759+
/// #![feature(allocator_api)]
760+
///
761+
/// use std::alloc::{AllocError, Allocator, Global, Layout};
762762
///
763763
/// fn main() {
764764
/// let layout = Layout::array::<u32>(16).expect("overflow cannot happen");
765+
///
765766
/// let vec = unsafe {
766-
/// let mem = alloc(layout).cast::<u32>();
767-
/// if mem.is_null() {
768-
/// return;
769-
/// }
767+
/// let mem = match Global.allocate(layout) {
768+
/// Ok(mem) => mem.cast::<u32>().as_ptr(),
769+
/// Err(AllocError) => return,
770+
/// };
770771
///
771772
/// mem.write(1_000_000);
772773
///
773-
/// Vec::from_raw_parts(mem, 1, 16)
774+
/// Vec::from_raw_parts_in(mem, 1, 16, Global)
774775
/// };
775776
///
776777
/// assert_eq!(vec, &[1_000_000]);

0 commit comments

Comments
 (0)