[PATCH] rust: alloc: add doc test for `Vec::from_elem`

Hsiu Che Yu
2026-04-26T03:47:02Z

Add a doc test for `Vec::from_elem` demonstrating basic usage and the
zero-length case.

Signed-off-by: Hsiu Che Yu <yu.whisper.personal@gmail.com>
---
rust/kernel/alloc/kvec.rs | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index ac8d6f763ae8..f5d213b4f715 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -792,6 +792,18 @@ pub fn extend_from_slice(&mut self, other: &[T], flags: Flags) -> Result<(), All
}

/// Create a new `Vec<T, A>` and extend it by `n` clones of `value`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// let v = KVec::from_elem(1, 5, GFP_KERNEL)?;
+ /// assert_eq!(&v, &[1, 1, 1, 1, 1]);
+ ///
+ /// let v = KVec::from_elem(1, 0, GFP_KERNEL)?;
+ /// assert!(v.is_empty());
+ ///
+ /// # Ok::<(), Error>(())
+ /// ```
pub fn from_elem(value: T, n: usize, flags: Flags) -> Result<Self, AllocError> {
let mut v = Self::with_capacity(n, flags)?;

---
base-commit: b4e07588e743c989499ca24d49e752c074924a9a
change-id: 20260426-doctest-alloc-kvec-from_elem-bb8eb3782978

Best regards,
--
Hsiu Che Yu <yu.whisper.personal@gmail.com>