- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为 leetcode same tree problem 编写了这段代码:
use std::cell::RefCell;
use std::rc::Rc;
// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}
struct Solution;
impl Solution {
pub fn is_same_tree(
p: Option<Rc<RefCell<TreeNode>>>,
q: Option<Rc<RefCell<TreeNode>>>,
) -> bool {
match (p, q) {
(None, None) => true,
(Some(p), Some(q)) if p.borrow().val == q.borrow().val => {
// this line won't work, it will cause BorrowMutError at runtime
// but the `a & b` version works
return (Self::is_same_tree(
p.borrow_mut().left.take(),
q.borrow_mut().left.take(),
)) && (Self::is_same_tree(
p.borrow_mut().right.take(),
q.borrow_mut().right.take(),
));
let a = Self::is_same_tree(p.borrow_mut().left.take(), q.borrow_mut().left.take());
let b =
Self::is_same_tree(p.borrow_mut().right.take(), q.borrow_mut().right.take());
a && b
}
_ => false,
}
}
}
fn main() {
let p = Some(Rc::new(RefCell::new(TreeNode {
val: 1,
left: Some(Rc::new(RefCell::new(TreeNode::new(2)))),
right: Some(Rc::new(RefCell::new(TreeNode::new(3)))),
})));
let q = Some(Rc::new(RefCell::new(TreeNode {
val: 1,
left: Some(Rc::new(RefCell::new(TreeNode::new(2)))),
right: Some(Rc::new(RefCell::new(TreeNode::new(3)))),
})));
println!("{:?}", Solution::is_same_tree(p, q));
}
playground
thread 'main' panicked at 'already borrowed: BorrowMutError', src/main.rs:39:23
我认为
&&
是一个短路运算符,这意味着两个表达式不会同时存在,因此两个可变引用不应该同时存在。
最佳答案
一个最小化的例子:
use std::cell::RefCell;
fn main() {
let x = RefCell::new(true);
*x.borrow_mut() && *x.borrow_mut();
}
thread 'main' panicked at 'already borrowed: BorrowMutError', src/main.rs:8:27
当您调用
RefCell::borrow_mut
,
RefMut
类型的临时值被退回。来自
the reference :
The drop scope of the temporary is usually the end of the enclosing statement.
Apart from lifetime extension, the temporary scope of an expression isthe smallest scope that contains the expression and is for one of thefollowing:
- The entire function body.
- A statement.
- The body of a
if
,while
orloop
expression.- The
else
block of anif
expression.- The condition expression of an
if
orwhile
expression, or amatch
guard.- The expression for a match arm.
- The second operand of a lazy boolean expression.
{
let t1 = x.borrow_mut();
*t1 && {
let t2 = x.borrow_mut();
*t2
}
}
这显示了双借是如何发生的。
let a = *x.borrow_mut();
a && *x.borrow_mut();
关于rust - 为什么 RefCell :borrow_mut result in a BorrowMutError when used on both sides of a short-circuiting boolean AND (&&)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64879078/
我正在使用 Adafruit 的 Circuit Playground Express,并且正在使用 Circuit Python 对其进行编程。 我想读取从通过 USB 连接 Circuit Pla
最初的问题是这样开始的。有6个州。在每个状态当 w=1 时移动到下一个状态,当 w=0 时则停留在当前状态。在每个状态下,使用标准的 7 位 LED 显示屏 (BCD) 显示一个数字。这些数字是 8
我试图找出如何在不使用顶级 if 的情况下在方案程序中进行“提前返回”。或 cond像构造。 (define (win b) (let* ((test (first (first b)))
我正在尝试使用 hystrix-javanica 为我的应用程序实现 hystrix。 我已经配置了 hystrix-configuration.properties 如下 hystrix.comma
我的团队最近决定使用 Moq 作为我们的模拟框架,因为它具有极大的灵 active 和高度可读的语法。由于我们是新手,我遇到了看似简单的问题——搜索(此处,谷歌等)发现大量关于最小起订量其他细微差别的
Steam显限时领取SUPER CIRCUIT BREAKERS - PAYNE 本周推出限时领取 DLC 《SUPER CIRCUIT BREAKERS - PAYNE》 DLC本体游戏并不是
我需要查看熔断是否已关闭,但我的政策处于政策包装中。如何在包裹中找到 CircuitBreakerPolicy(一个或多个)?有没有更好的方法来了解是否有任何断路器对给定策略开放? 最佳答案 根据 P
我们已经将 SS 作为我们的 REST 服务器平台并喜欢它 - 我面临的反复出现的问题之一是处理因某种合法原因(错误、缺少强制 header 等)而“短路”的日志记录请求。 我发现我必须向许多不同的代
当我尝试使用 Arduino IDE 将代码上传到我的 CPE 时遇到问题。我安装了 CircuitPython 3.0.0,并且尝试将闪烁草图上传到我的 CPE,但它抛出了此错误。 No devic
这个问题在这里已经有了答案: 关闭 13 年前。 考虑一下 Class* p = NULL; if( p != NULL && p->Method() == OK ){ // stuff } 在
例如: def foo(): print 'foo' return 1 if any([f() for f in [foo]*3]): print 'bar' 我认为上面的代码应
环境:Springboot2.3.12.RELEASE + cloud-netflix-hystrix2.2.10.RELEASE 简介 SpringCloud Circuit break
有时我会看到一个 CallNotPermittedException,其中有一条消息说断路器处于 HALF_OPEN 状态。但我不明白它在那种状态下是如何工作的。我用一个模拟服务器编写了一个测试,我在
我正在尝试了解尚未合并到标准中的 Concepts Lite TS。我对概念体中短路析取的行为感到困惑。 这是一个小例子: #include #include template concept
我想知道 JavaScript 是否具有像 C# 中的 && 运算符那样的“短路”求值。如果没有,我想知道是否有适合采用的解决方法。 最佳答案 是的,JavaScript 有“短路”求值。 if (t
环境:Springboot2.3.12.RELEASE + cloud-netflix-hystrix2.2.10.RELEASE 简介 SpringCloud Circuit break
当我们收到由于耗尽连接池而导致的超时异常时,我们正在尝试为我们的数据库逻辑实现重试策略。当我们在短时间内出现异常大的事件峰值时,就会发生这种情况。我们增加了最大池大小以尽量避免这种情况,但我们也希望将
我正在调整一种分析大量数据的算法,以使其运行得更快一些。它大量使用 clone 函数,如下所示: const Object* clone() const { if (this != INVAL
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 4 年前。 Improve
在我的项目中,我使用 Worker进行一些与事件相关的文件处理并避免阻塞其他事件的处理。我需要的是能够在工作人员完成时将事件(需要文件处理)置于待命状态(其他组件不会进一步处理),然后恢复处理序列。
我是一名优秀的程序员,十分优秀!