Skip to content

Commit 9cf2516

Browse files
committed
doc(array,vec): add notes about side effects when empty-initializing
1 parent d32c320 commit 9cf2516

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

Diff for: library/alloc/src/macros.rs

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
/// to the same boxed integer value, not five references pointing to independently
3030
/// boxed integers.
3131
///
32+
/// Also, note that `[T; 0]` is a valid initializer. This will initialize (or call)
33+
/// `T` but not populate the vector with it, so be mindful of side effects.
34+
///
3235
/// [`Vec`]: crate::vec::Vec
3336
#[cfg(not(test))]
3437
#[macro_export]

Diff for: library/std/src/primitive_docs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@ mod prim_pointer {}
489489
/// * A repeat expression `[x; N]`, which produces an array with `N` copies of `x`.
490490
/// The type of `x` must be [`Copy`].
491491
///
492+
/// Note that `[x; 0]` is a valid repeat expression. This will produce an empty array
493+
/// but will also initialize (or call) `x`, which may produce side effects.
494+
///
492495
/// Arrays of *any* size implement the following traits if the element type allows it:
493496
///
494497
/// - [`Copy`]

0 commit comments

Comments
 (0)