Enum std::ops::ControlFlow

1.55.0 · source ·
pub enum ControlFlow<B, C = ()> {
    Continue(C),
    Break(B),
}
Expand description

用于告诉操作是应该提前退出还是像往常一样继续操作。

在将您希望用户能够选择是否提前退出的事物 (例如图形遍历或访问者) 公开时使用。 有了枚举可以使它更清晰 - 不必再奇怪 “wait, what did false mean again?” 了 - 并允许包含一个值。

OptionResult 类似,此枚举可与 ? 运算符一起使用,以便在 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);
Run

Variants§

§

Continue(C)

照常进行下一阶段的操作。

§

Break(B)

退出操作而不运行后续阶段。

Implementations§

source§

impl<B, C> ControlFlow<B, C>

1.59.0 · source

pub fn is_break(&self) -> bool

如果这是 Break 变体,则返回 true

Examples
use std::ops::ControlFlow;

assert!(ControlFlow::<i32, String>::Break(3).is_break());
assert!(!ControlFlow::<String, i32>::Continue(3).is_break());
Run
1.59.0 · source

pub fn is_continue(&self) -> bool

如果这是 Continue 变体,则返回 true

Examples
use std::ops::ControlFlow;

assert!(!ControlFlow::<i32, String>::Break(3).is_continue());
assert!(ControlFlow::<String, i32>::Continue(3).is_continue());
Run
source

pub fn break_value(self) -> Option<B>

🔬This is a nightly-only experimental API. (control_flow_enum #75744)

如果 ControlFlowBreak,则将 ControlFlow 转换为 Some,否则为 None

Examples
#![feature(control_flow_enum)]
use std::ops::ControlFlow;

assert_eq!(ControlFlow::<i32, String>::Break(3).break_value(), Some(3));
assert_eq!(ControlFlow::<String, i32>::Continue(3).break_value(), None);
Run
source

pub 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)

Maps ControlFlow<B, C>ControlFlow<T, C> 通过在中断值 (如果存在) 上应用函数来实现。

source

pub fn continue_value(self) -> Option<C>

🔬This is a nightly-only experimental API. (control_flow_enum #75744)

ControlFlow 转换为 Option,如果 ControlFlowContinue,则为 Some,否则为 None

Examples
#![feature(control_flow_enum)]
use std::ops::ControlFlow;

assert_eq!(ControlFlow::<i32, String>::Break(3).continue_value(), None);
assert_eq!(ControlFlow::<String, i32>::Continue(3).continue_value(), Some(3));
Run
source

pub 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)

Maps ControlFlow<B, C>ControlFlow<B, T> 通过将函数应用于 continue 值,以防它存在。

source§

impl<R> ControlFlow<R, <R as Try>::Output>where R: Try,

This impl block contains no items.

这些仅用作实现迭代器适配器的一部分。 它们具有普通的名称和不明显的语义,因此目前还没有走上潜在的稳定之路。

Trait Implementations§

source§

impl<B, C> Clone for ControlFlow<B, C>where B: Clone, C: Clone,

source§

fn clone(&self) -> ControlFlow<B, C>

返回值的副本。 Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

source 执行复制分配。 Read more
source§

impl<B, C> Debug for ControlFlow<B, C>where B: Debug, C: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

使用给定的格式化程序格式化该值。 Read more
source§

impl<B, C> FromResidual<<ControlFlow<B, C> as Try>::Residual> for ControlFlow<B, C>

source§

fn from_residual(residual: ControlFlow<B, Infallible>) -> ControlFlow<B, C>

🔬This is a nightly-only experimental API. (try_trait_v2 #84277)
从兼容的 Residual 类型构造类型。 Read more
source§

impl<B, C> Hash for ControlFlow<B, C>where B: Hash, C: Hash,

source§

fn hash<__H>(&self, state: &mut __H)where __H: Hasher,

将该值输入给定的 HasherRead more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

将这种类型的切片送入给定的 Hasher 中。 Read more
source§

impl<B, C> PartialEq<ControlFlow<B, C>> for ControlFlow<B, C>where B: PartialEq<B>, C: PartialEq<C>,

source§

fn eq(&self, other: &ControlFlow<B, C>) -> bool

此方法测试 selfother 值是否相等,并由 == 使用。
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

此方法测试 !=。 默认实现几乎总是足够的,并且不应在没有充分理由的情况下被覆盖。
source§

impl<B, C> Residual<C> for ControlFlow<B, Infallible>

§

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>

§

type Output = C

🔬This is a nightly-only experimental API. (try_trait_v2 #84277)
当不短路时,? 产生的值的类型。
§

type Residual = ControlFlow<B, Infallible>

🔬This is a nightly-only experimental API. (try_trait_v2 #84277)
短路时作为 ? 的一部分传递给 FromResidual::from_residual 的值的类型。 Read more
source§

fn from_output(output: <ControlFlow<B, C> as Try>::Output) -> ControlFlow<B, C>

🔬This is a nightly-only experimental API. (try_trait_v2 #84277)
从它的 Output 类型构造类型。 Read more
source§

fn branch( self ) -> ControlFlow<<ControlFlow<B, C> as Try>::Residual, <ControlFlow<B, C> as Try>::Output>

🔬This is a nightly-only experimental API. (try_trait_v2 #84277)
? 来决定操作符是应该生成一个值 (因为它返回了 ControlFlow::Continue),还是将一个值传播回调用者 (因为它返回了 ControlFlow::Break)。 Read more
source§

impl<B, C> Copy for ControlFlow<B, C>where B: Copy, C: Copy,

source§

impl<B, C> Eq for ControlFlow<B, C>where B: Eq, C: Eq,

source§

impl<B, C> StructuralEq for ControlFlow<B, C>

source§

impl<B, C> StructuralPartialEq for ControlFlow<B, C>

Auto Trait Implementations§

§

impl<B, C> RefUnwindSafe for ControlFlow<B, C>where B: RefUnwindSafe, C: RefUnwindSafe,

§

impl<B, C> Send for ControlFlow<B, C>where B: Send, C: Send,

§

impl<B, C> Sync for ControlFlow<B, C>where B: Sync, C: Sync,

§

impl<B, C> Unpin for ControlFlow<B, C>where B: Unpin, C: Unpin,

§

impl<B, C> UnwindSafe for ControlFlow<B, C>where B: UnwindSafe, C: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

获取 selfTypeIdRead more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

从拥有的值中一成不变地借用。 Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

从拥有的值中借用。 Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

返回未更改的参数。

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

调用 U::from(self)

也就是说,这种转换是 From<T> for U 实现选择执行的任何操作。

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

获得所有权后的结果类型。
source§

fn to_owned(&self) -> T

从借用的数据创建拥有的数据,通常是通过克隆。 Read more
source§

fn clone_into(&self, target: &mut T)

使用借来的数据来替换拥有的数据,通常是通过克隆。 Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

发生转换错误时返回的类型。
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

执行转换。
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

发生转换错误时返回的类型。
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

执行转换。