gpt4 book ai didi

multithreading - 在 Rust 的线程中从闭包内传播错误

转载 作者:行者123 更新时间:2023-11-29 08:28:49 24 4
gpt4 key购买 nike

<分区>

我想从调用 thread::spawn 的闭包内部调用的函数传播错误。

我已经尝试使用 JoinHandle 来捕获 thread::spawn 的结果,但是我在这样做时遇到了各种错误。

fn start_server(args) -> Result<(), Box<dyn std::error::Error>> {
...
thread::spawn(move || {
// I want to do this (put a ? after run_server)
run_server(args)?;
...
}
...
});
fn run_server(args) -> Result<(), std::io::Error> {
...
}

我收到这条消息

|             run_server(args)?;
| ^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `()`
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`

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