 Rust 程序设计语言 简体中文版 1.85.0模。 如下是本章将要涉及到的内容: • 如何创建线程来同时运行多段代码。 • 消息传递(Message passing)并发,其中信道(channel)被用来在线程间传递消息。 • 共享状态(Shared state)并发,其中多个线程可以访问同一片数据。 • Sync 和 Send trait,将 Rust 的并发保证扩展到用户定义的以及标准库提供的类型中。 354/562Rust 程序设计语言 待所有线程结束。然后,主线程会获取锁,并打印出程序的结果。 之前提示过,这个例子不能编译。现在让我们看看为什么! $ cargo run Compiling shared-state v0.1.0 (file:///projects/shared-state) error[E0382]: borrow of moved value: `counter` --> src/main.rs:21:29 more information about this error, try `rustc --explain E0382`. error: could not compile `shared-state` (bin "shared-state") due to 1 previous error 错误信息表明 counter 值在上一次循环中被移动了。所以 Rust 告诉我们,不能将 counter 锁0 码力 | 562 页 | 3.23 MB | 26 天前3 Rust 程序设计语言 简体中文版 1.85.0模。 如下是本章将要涉及到的内容: • 如何创建线程来同时运行多段代码。 • 消息传递(Message passing)并发,其中信道(channel)被用来在线程间传递消息。 • 共享状态(Shared state)并发,其中多个线程可以访问同一片数据。 • Sync 和 Send trait,将 Rust 的并发保证扩展到用户定义的以及标准库提供的类型中。 354/562Rust 程序设计语言 待所有线程结束。然后,主线程会获取锁,并打印出程序的结果。 之前提示过,这个例子不能编译。现在让我们看看为什么! $ cargo run Compiling shared-state v0.1.0 (file:///projects/shared-state) error[E0382]: borrow of moved value: `counter` --> src/main.rs:21:29 more information about this error, try `rustc --explain E0382`. error: could not compile `shared-state` (bin "shared-state") due to 1 previous error 错误信息表明 counter 值在上一次循环中被移动了。所以 Rust 告诉我们,不能将 counter 锁0 码力 | 562 页 | 3.23 MB | 26 天前3
共 1 条
- 1













