我正在try 编译类似以下内容的内容:
use anyhow::Result;
use std::sync::{mpsc, Mutex, OnceLock};
use std::cell::Cell;
use std::fmt;
static COMS: OnceLock<Mutex<Coms>> = OnceLock::new();
struct Coms {
tx: Cell<Option<mpsc::Sender<Msg>>>,
rx: Cell<Option<mpsc::Receiver<Msg>>>,
}
enum Msg {}
fn bar() -> Result<()> {
let (tx, rx) = mpsc::channel();
COMS.set(
Mutex::new(
Coms {
tx: Cell::new(Some(tx)),
rx: Cell::new(Some(rx)),
}
)
);
//)?;
Ok(())
}
fn main() {
let _ = bar();
}
我收到关于未使用的结果的预期警告:
warning: unused `Result` that must be used
--> src/main.rs:17:5
|
17 | / COMS.set(
18 | | Mutex::new(
19 | | Coms {
20 | | tx: Cell::new(Some(tx)),
... |
23 | | )
24 | | );
| |_____^
但是,如果我将行与);
替换为)?;
,则会得到如下错误:
error[E0277]: the trait bound `Mutex<Coms>: std::error::Error` is not satisfied
--> src/main.rs:25:6
|
25 | )?;
| ^ the trait `std::error::Error` is not implemented for `Mutex<Coms>`
|
OnceLock::set()
的documentation表示它返回Result
,那么为什么在第二种情况下它看起来像是返回Mutex<Coms>
呢?
为什么Rustc似乎认为第一个 case 是Result
分(预计),第二个 case 是Mutex<Coms>
分?这是使用rustc 1.71.0
.我一定是误解了编译器想要告诉我的内容.