]> git.lizzy.rs Git - rust.git/blobdiff - src/liballoc/boxed.rs
impl From<[T; N]> for Box<[T]>
[rust.git] / src / liballoc / boxed.rs
index 8ef6090c743a9e6daf052670d370ef8a08e7a719..8cc6f04c0653a401dbb5efdb70b3c745f4403ee1 100644 (file)
@@ -865,6 +865,25 @@ fn from(s: Box<str>) -> Self {
     }
 }
 
+#[stable(feature = "box_from_array", since = "1.45.0")]
+impl<T, const N: usize> From<[T; N]> for Box<[T]>
+where
+    [T; N]: LengthAtMost32,
+{
+    /// Converts a `[T; N]` into a `Box<[T]>`
+    ///
+    /// This conversion moves the array to newly heap-allocated memory.
+    ///
+    /// # Examples
+    /// ```rust
+    /// let boxed: Box<[u8]> = Box::from([4, 2]);
+    /// println!("{:?}", boxed);
+    /// ```
+    fn from(array: [T; N]) -> Box<[T]> {
+        box array
+    }
+}
+
 #[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
 impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]>
 where