From 9cf2516251c5379d3e8429342e8a392df81b9aaa Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Thu, 10 Dec 2020 17:47:28 -0500 Subject: [PATCH 1/2] doc(array,vec): add notes about side effects when empty-initializing --- library/alloc/src/macros.rs | 3 +++ library/std/src/primitive_docs.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/library/alloc/src/macros.rs b/library/alloc/src/macros.rs index a992d768d6314..bbff1d3dc4d2e 100644 --- a/library/alloc/src/macros.rs +++ b/library/alloc/src/macros.rs @@ -29,6 +29,9 @@ /// to the same boxed integer value, not five references pointing to independently /// boxed integers. /// +/// Also, note that `[T; 0]` is a valid initializer. This will initialize (or call) +/// `T` but not populate the vector with it, so be mindful of side effects. +/// /// [`Vec`]: crate::vec::Vec #[cfg(not(test))] #[macro_export] diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index 55171ef2292d7..b22adbbe3d356 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -489,6 +489,9 @@ mod prim_pointer {} /// * A repeat expression `[x; N]`, which produces an array with `N` copies of `x`. /// The type of `x` must be [`Copy`]. /// +/// Note that `[x; 0]` is a valid repeat expression. This will produce an empty array +/// but will also initialize (or call) `x`, which may produce side effects. +/// /// Arrays of *any* size implement the following traits if the element type allows it: /// /// - [`Copy`] From d986924eb13a103fc79c474eded47a663c91bb7f Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Fri, 11 Dec 2020 10:09:40 -0500 Subject: [PATCH 2/2] doc: apply suggestions --- library/alloc/src/macros.rs | 5 +++-- library/std/src/primitive_docs.rs | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/library/alloc/src/macros.rs b/library/alloc/src/macros.rs index bbff1d3dc4d2e..7d4eff6185dbe 100644 --- a/library/alloc/src/macros.rs +++ b/library/alloc/src/macros.rs @@ -29,8 +29,9 @@ /// to the same boxed integer value, not five references pointing to independently /// boxed integers. /// -/// Also, note that `[T; 0]` is a valid initializer. This will initialize (or call) -/// `T` but not populate the vector with it, so be mindful of side effects. +/// Also, note that `vec![expr; 0]` is allowed, and produces an empty vector. +/// This will still evaluate `expr`, however, and immediately drop the resulting value, so +/// be mindful of side effects. /// /// [`Vec`]: crate::vec::Vec #[cfg(not(test))] diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index b22adbbe3d356..7aca5451ebc20 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -489,8 +489,9 @@ mod prim_pointer {} /// * A repeat expression `[x; N]`, which produces an array with `N` copies of `x`. /// The type of `x` must be [`Copy`]. /// -/// Note that `[x; 0]` is a valid repeat expression. This will produce an empty array -/// but will also initialize (or call) `x`, which may produce side effects. +/// Note that `[expr; 0]` is allowed, and produces an empty array. +/// This will still evaluate `expr`, however, and immediately drop the resulting value, so +/// be mindful of side effects. /// /// Arrays of *any* size implement the following traits if the element type allows it: ///