Enum core::ops::ControlFlow
1.55.0 · source · pub enum ControlFlow<B, C = ()> {
Continue(C),
Break(B),
}
Expand description
用于告诉操作是应该提前退出还是像往常一样继续操作。
在将您希望用户能够选择是否提前退出的事物 (例如图形遍历或访问者) 公开时使用。
有了枚举可以使它更清晰 - 不必再奇怪 “wait, what did false
mean again?” 了 - 并允许包含一个值。
与 Option
和 Result
类似,此枚举可与 ?
运算符一起使用,以便在 Break
变体存在时立即返回,或者以其他方式正常继续使用 Continue
变体中的值。
Examples
从 Iterator::try_for_each
提前退出:
use std::ops::ControlFlow;
let r = (2..100).try_for_each(|x| {
if 403 % x == 0 {
return ControlFlow::Break(x)
}
ControlFlow::Continue(())
});
assert_eq!(r, ControlFlow::Break(13));
Run一个基本的树遍历:
use std::ops::ControlFlow;
pub struct TreeNode<T> {
value: T,
left: Option<Box<TreeNode<T>>>,
right: Option<Box<TreeNode<T>>>,
}
impl<T> TreeNode<T> {
pub fn traverse_inorder<B>(&self, f: &mut impl FnMut(&T) -> ControlFlow<B>) -> ControlFlow<B> {
if let Some(left) = &self.left {
left.traverse_inorder(f)?;
}
f(&self.value)?;
if let Some(right) = &self.right {
right.traverse_inorder(f)?;
}
ControlFlow::Continue(())
}
fn leaf(value: T) -> Option<Box<TreeNode<T>>> {
Some(Box::new(Self { value, left: None, right: None }))
}
}
let node = TreeNode {
value: 0,
left: TreeNode::leaf(1),
right: Some(Box::new(TreeNode {
value: -1,
left: TreeNode::leaf(5),
right: TreeNode::leaf(2),
}))
};
let mut sum = 0;
let res = node.traverse_inorder(&mut |val| {
if *val < 0 {
ControlFlow::Break(*val)
} else {
sum += *val;
ControlFlow::Continue(())
}
});
assert_eq!(res, ControlFlow::Break(-1));
assert_eq!(sum, 6);
RunVariants§
Implementations§
source§impl<B, C> ControlFlow<B, C>
impl<B, C> ControlFlow<B, C>
1.59.0 · sourcepub fn is_continue(&self) -> bool
pub fn is_continue(&self) -> bool
sourcepub fn break_value(self) -> Option<B>
🔬This is a nightly-only experimental API. (control_flow_enum
#75744)
pub fn break_value(self) -> Option<B>
control_flow_enum
#75744)sourcepub fn map_break<T, F>(self, f: F) -> ControlFlow<T, C>where
F: FnOnce(B) -> T,
🔬This is a nightly-only experimental API. (control_flow_enum
#75744)
pub fn map_break<T, F>(self, f: F) -> ControlFlow<T, C>where F: FnOnce(B) -> T,
control_flow_enum
#75744)Maps ControlFlow<B, C>
到 ControlFlow<T, C>
通过在中断值 (如果存在) 上应用函数来实现。
sourcepub fn continue_value(self) -> Option<C>
🔬This is a nightly-only experimental API. (control_flow_enum
#75744)
pub fn continue_value(self) -> Option<C>
control_flow_enum
#75744)sourcepub fn map_continue<T, F>(self, f: F) -> ControlFlow<B, T>where
F: FnOnce(C) -> T,
🔬This is a nightly-only experimental API. (control_flow_enum
#75744)
pub fn map_continue<T, F>(self, f: F) -> ControlFlow<B, T>where F: FnOnce(C) -> T,
control_flow_enum
#75744)Maps ControlFlow<B, C>
到 ControlFlow<B, T>
通过将函数应用于 continue 值,以防它存在。
Trait Implementations§
source§impl<B: Clone, C: Clone> Clone for ControlFlow<B, C>
impl<B: Clone, C: Clone> Clone for ControlFlow<B, C>
source§fn clone(&self) -> ControlFlow<B, C>
fn clone(&self) -> ControlFlow<B, C>
返回值的副本。 Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
从
source
执行复制分配。 Read moresource§impl<B, C> FromResidual<<ControlFlow<B, C> as Try>::Residual> for ControlFlow<B, C>
impl<B, C> FromResidual<<ControlFlow<B, C> as Try>::Residual> for ControlFlow<B, C>
source§fn from_residual(residual: ControlFlow<B, Infallible>) -> Self
fn from_residual(residual: ControlFlow<B, Infallible>) -> Self
🔬This is a nightly-only experimental API. (
try_trait_v2
#84277)从兼容的
Residual
类型构造类型。 Read moresource§impl<B: PartialEq, C: PartialEq> PartialEq<ControlFlow<B, C>> for ControlFlow<B, C>
impl<B: PartialEq, C: PartialEq> PartialEq<ControlFlow<B, C>> for ControlFlow<B, C>
source§impl<B, C> Residual<C> for ControlFlow<B, Infallible>
impl<B, C> Residual<C> for ControlFlow<B, Infallible>
§type TryType = ControlFlow<B, C>
type TryType = ControlFlow<B, C>
🔬This is a nightly-only experimental API. (
try_trait_v2_residual
#91285)此元函数的 “return” 类型。
source§impl<B, C> Try for ControlFlow<B, C>
impl<B, C> Try for ControlFlow<B, C>
§type Residual = ControlFlow<B, Infallible>
type Residual = ControlFlow<B, Infallible>
🔬This is a nightly-only experimental API. (
try_trait_v2
#84277)source§fn from_output(output: Self::Output) -> Self
fn from_output(output: Self::Output) -> Self
🔬This is a nightly-only experimental API. (
try_trait_v2
#84277)从它的
Output
类型构造类型。 Read moresource§fn branch(self) -> ControlFlow<Self::Residual, Self::Output>
fn branch(self) -> ControlFlow<Self::Residual, Self::Output>
🔬This is a nightly-only experimental API. (
try_trait_v2
#84277)在
?
来决定操作符是应该生成一个值 (因为它返回了 ControlFlow::Continue
),还是将一个值传播回调用者 (因为它返回了 ControlFlow::Break
)。 Read more