小编ibs*_*bse的帖子

如何从 Tokio 的非主线程运行异步任务?

use std::thread;
use tokio::task; // 0.3.4

#[tokio::main]
async fn main() {
    thread::spawn(|| {
        task::spawn(async {
            println!("123");
        });
    })
    .join();
}
Run Code Online (Sandbox Code Playgroud)

编译时我收到警告:

warning: unused `std::result::Result` that must be used
  --> src/main.rs:6:5
   |
6  | /     thread::spawn(|| {
7  | |         task::spawn(async {
8  | |             println!("123");
9  | |         });
10 | |     })
11 | |     .join();
   | |____________^
   |
   = note: `#[warn(unused_must_use)]` on by default
   = note: this `Result` may be an `Err` variant, which should be handled
Run Code Online (Sandbox Code Playgroud)

执行时出现错误: …

asynchronous rust rust-tokio

12
推荐指数
2
解决办法
6008
查看次数

用三元运算符初始化 boost::optional

是一种初始化可选的方法,如:

bool conditional = true;
boost::optional<int> opt = conditional ? 5 : boost::none;
Run Code Online (Sandbox Code Playgroud)

为什么会出现错误?:

main.cpp:31:31: error: operands to ?: have different types ‘int’ and ‘const boost::none_t’
boost::make_optional(cond ? 5 : boost::none);
      |                          ~~~~~^~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

用简单的if else我可以做到这一点:

boost::optional<int> opt;
if (cond)
    opt = 5;
else
    opt = boost::none;
Run Code Online (Sandbox Code Playgroud)

c++ boost optional c++11

3
推荐指数
1
解决办法
194
查看次数

标签 统计

asynchronous ×1

boost ×1

c++ ×1

c++11 ×1

optional ×1

rust ×1

rust-tokio ×1