From 343d0aa5d957eaacc5dc17f0f9bdd53779600ea2 Mon Sep 17 00:00:00 2001 From: "loongtao.zhang" Date: Wed, 22 Oct 2025 20:57:58 +0800 Subject: [PATCH] feat(option): add Option::clear method Signed-off-by: loongtao.zhang --- library/core/src/option.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/core/src/option.rs b/library/core/src/option.rs index e3c4758bc6af5..aa6ffaff5b85e 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1842,6 +1842,20 @@ impl Option { mem::replace(self, None) } + /// Clear the value of the option. + /// + /// # Examples + /// + /// ``` + /// let mut x = Some(2); + /// x.clear(); + /// assert_eq!(x, None); + /// ``` + #[inline(always)] + pub fn clear(&mut self) { + *self = None; + } + /// Takes the value out of the option, but only if the predicate evaluates to /// `true` on a mutable reference to the value. ///