]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
rust: alloc: kvec: add doc example for as_slice method
authorHui Zhu <zhuhui@kylinos.cn>
Thu, 31 Jul 2025 02:50:06 +0000 (10:50 +0800)
committerDanilo Krummrich <dakr@kernel.org>
Wed, 13 Aug 2025 16:52:31 +0000 (18:52 +0200)
Add a practical usage example to the documentation of KVec::as_slice()
showing how to:
Create a new KVec.
Push elements into it.
Convert to a slice via as_slice().

Co-developed-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Kunwu Chan <chentao@kylinos.cn>
Reviewed-by: David Gow <davidgow@google.com>
Link: https://lore.kernel.org/r/4e7f396f38ed8a780f863384bfc3d7de135ef3ea.1753929369.git.zhuhui@kylinos.cn
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
rust/kernel/alloc/kvec.rs

index 3c72e0bdddb871b1d8b2cd0aea08f57ef33e6c46..fa04cc0987d6bcd44af3f1dc6ae7ee5bf907974c 100644 (file)
@@ -224,6 +224,16 @@ where
     }
 
     /// Returns a slice of the entire vector.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let mut v = KVec::new();
+    /// v.push(1, GFP_KERNEL)?;
+    /// v.push(2, GFP_KERNEL)?;
+    /// assert_eq!(v.as_slice(), &[1, 2]);
+    /// # Ok::<(), Error>(())
+    /// ```
     #[inline]
     pub fn as_slice(&self) -> &[T] {
         self