gpt4 book ai didi

rust - 不能可变地借用模式守卫 [E0301]

转载 作者:行者123 更新时间:2023-11-29 08:12:51 25 4
gpt4 key购买 nike

unsafe fn get_sync_handle(pid: u32) -> Option<HANDLE>
{
let raw_h = OpenProcess(SYNCHRONIZE, 0, pid as DWORD);
match raw_h
{
x if x >= 0 as HANDLE => None,
x => Some(x)
}
}

src\lib.rs:411:19: 411:23 error: cannot mutably borrow in a pattern guard [E0301] src\lib.rs:411 x if x >= zero => None

有人可以向我解释这个错误的原因吗?虽然我知道这是一个非常人为的例子,但我不明白为什么编译器会提示,我并没有试图修改任何东西,而且 raw_h 本身是不可变的。

最佳答案

这是一个独立的例子:

type Handle = *mut ();

fn main() {
let foo = 0 as Handle;
match foo {
x if x >= 0 as Handle => None,
x => Some(x)
};
}

记下错误信息和它指向的地方:

<anon>:6:19: 6:30 error: cannot mutably borrow in a pattern guard [E0301]
<anon>:6 x if x >= 0 as Handle => None,
^~~~~~~~~~~

其实是被吐槽的“null”。你可以翻转支票,然后比赛守卫中就不会有任何可变性。

type Handle = *mut ();

fn main() {
let foo = 0 as Handle;
match foo {
x if x as isize >= 0 => None,
x => Some(x)
};
}

关于rust - 不能可变地借用模式守卫 [E0301],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30197745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com