8000 Rollup of 4 pull requests by matthiaskrgr · Pull Request #90282 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Rollup of 4 pull requests #90282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 26, 2021
Prev Previous commit
Next Next commit
Update control_flow.rs
Fix and extent ControlFlow `traverse_inorder` example

1. The existing example compiles on its own, but any usage fails to be monomorphised and so doesn't compile. Fix that by using Fn trait instead of FnMut.
2. Added an example usage of `traverse_inorder` showing how we can terminate the traversal early.

Fixes #90063
  • Loading branch information
yanok committed Oct 23, 2021
commit 508fadab16ec36c57daa8f0361db60848d31c0f7
29 changes: 26 additions & 3 deletions library/core/src/ops/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,40 @@ use crate::{convert, ops};
/// }
///
/// impl<T> TreeNode<T> {
/// pub fn traverse_inorder<B>(&self, mut f: impl FnMut(&T) -> ControlFlow<B>) -> ControlFlow<B> {
/// pub fn traverse_inorder<B>(&self, f: &impl Fn(&T) -> ControlFlow<B>) -> ControlFlow<B> {
/// if let Some(left) = &self.left {
/// left.traverse_inorder(&mut f)?;
/// left.traverse_inorder(f)?;
/// }
/// f(&self.value)?;
/// if let Some(right) = &self.right {
/// right.traverse_inorder(&mut f)?;
/// right.traverse_inorder(f)?;
/// }
/// ControlFlow::Continue(())
/// }
/// }
///
/// let node = TreeNode {
/// value: 0,
/// left: Some(Box::new(TreeNode {
/// value: 1,
/// left: None,
/// right: None
/// })),
/// right: Some(Box::new(TreeNode {
/// value: 2,
/// left: None,
/// right: None
/// }))
/// };
///
/// node.traverse_inorder(& |val| {
/// println!("{}", val);
/// if *val <= 0 {
/// ControlFlow::Break(())
/// } else {
/// ControlFlow::Continue(())
/// }
/// });
/// ```
#[stable(feature = "control_flow_enum_type", since = "1.55.0")]
#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down
0