`&Borrowed` 所需的类型注释

Jer*_*uan 6 rust

我正在尝试在https://rustgym.com/leetcode/98中编译 LeetCode 问题 98 的 rust 代码 ,但是,我在这一行中收到错误let node = node.borrow();::

type annotations needed for `&Borrowed`
type must be known at this point
rustcE0282
s0098_validate_binary_search_tree.rs(66, 17): consider giving `node` the explicit type `&Borrowed`, where the type parameter `Borrowed` is specified
Run Code Online (Sandbox Code Playgroud)

不过Leetcode编译是没有问题的。这是代码。

use std::rc::Rc;
use std::cell::RefCell;
type TreeLink = Option<Rc<RefCell<TreeNode>>>;
trait Inorder {
    fn inorder(&self, visit: &mut dyn FnMut(i32));
}
impl Inorder for TreeLink {
    fn inorder(&self, visit: &mut dyn FnMut(i32)) {
        if let Some(node) = self {
            let node = node.borrow();
            Self::inorder(&node.left, visit);
            visit(node.val);
            Self::inorder(&node.right, visit);
        }    
    }
}

impl Solution {
    pub fn is_valid_bst(root: TreeLink) -> bool {
        let mut prev: Option<i32> = None;
        let mut res = true;
        root.inorder(&mut |x| {
            if let Some(y) = prev {
                if x <= y {
                    res = false;
                }
            }
            prev = Some(x);
        });
        res
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 rustc 1.55.0 (c8dfcfe04 2021-09-06)。我认为这可能是编译器中的一个错误。

小智 5

  1. 你的代码没问题。我通过你的代码通过了98',没有任何改变。
    在此输入图像描述

  2. 我通过添加冗余代码遇到了同样的问题use std::borrow::Borrow;。所以删除后就use std::borrow::Borrow;一切正常了~
    在此输入图像描述


小智 0

解决方案https://rustgym.com/leetcode/98使用 rustgym_util::*; 定义 TreeNode、TreeLink 等。

struct Solution;
//
//--Begin rustgym_util:: def----------------------------------
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct TreeNode {
    pub val: i32,
    pub left: TreeLink,
    pub right: TreeLink,
}
pub type TreeLink = Option<Rc<RefCell<TreeNode>>>;

#[macro_export]
macro_rules! tree {
    ($e:expr) => {
        TreeLink::leaf($e)
    };
    ($e:expr, $l:expr, $r:expr) => {
        TreeLink::branch($e, $l, $r)
    };
}

use std::cell::RefCell;
use std::rc::Rc;

pub trait TreeMaker {
    fn branch(val: i32, left: TreeLink, right: TreeLink) -> TreeLink {
        Some(Rc::new(RefCell::new(TreeNode { val, left, right })))
    }
    fn leaf(val: i32) -> TreeLink {
        Some(Rc::new(RefCell::new(TreeNode {
            val,
            left: None,
            right: None,
        })))
    }
}

impl TreeMaker for TreeLink {}
//
//--Endrustgym_util:: def----------------------------------

trait Inorder {
    fn inorder(&self, visit: &mut dyn FnMut(i32));
}

impl Inorder for TreeLink {
    fn inorder(&self, visit: &mut dyn FnMut(i32)) {
        if let Some(node) = self {
            let node = node.borrow();
            Self::inorder(&node.left, visit);
            visit(node.val);
            Self::inorder(&node.right, visit);
        }
    }
}

impl Solution {
    fn is_valid_bst(root: TreeLink) -> bool {
        let mut prev: Option<i32> = None;
        let mut res = true;
        root.inorder(&mut |x| {
            if let Some(y) = prev {
                if x <= y {
                    res = false;
                }
            }
            prev = Some(x);
        });
        res
    }
}

#[test]
fn test() {
    let root = tree!(2, tree!(1), tree!(3));
    let res = true;
    assert_eq!(Solution::is_valid_bst(root), res);
    let root = tree!(5, tree!(1), tree!(4, tree!(3), tree!(6)));
    let res = false;
    assert_eq!(Solution::is_valid_bst(root), res);
}
Run Code Online (Sandbox Code Playgroud)

你这边是如何定义TreeNode的?