rust

(tokio::spawn) borrowed value does not live long enough — argument requires that `sleepy` is borrowed for `'static`

≡放荡痞女 提交于 2021-02-02 09:56:35
问题 This MWE shows the use of tokio::spawn in for in loop. The commented code sleepy_futures.push(sleepy.sleep_n(2)); works fine, but does not run/poll the async function. Basically, I would like to run a bunch of async functions at the same time. I am happy to change the implementation of Sleepy or use another library/technique. pub struct Sleepy; impl Sleepy { pub async fn sleep_n(self: &Self, n: u64) -> String { sleep(Duration::from_secs(n)); "test".to_string() } } #[tokio::main(core_threads =

(tokio::spawn) borrowed value does not live long enough — argument requires that `sleepy` is borrowed for `'static`

≯℡__Kan透↙ 提交于 2021-02-02 09:53:08
问题 This MWE shows the use of tokio::spawn in for in loop. The commented code sleepy_futures.push(sleepy.sleep_n(2)); works fine, but does not run/poll the async function. Basically, I would like to run a bunch of async functions at the same time. I am happy to change the implementation of Sleepy or use another library/technique. pub struct Sleepy; impl Sleepy { pub async fn sleep_n(self: &Self, n: u64) -> String { sleep(Duration::from_secs(n)); "test".to_string() } } #[tokio::main(core_threads =

Defining a method for a struct only when a field is a certain enum variant?

别等时光非礼了梦想. 提交于 2021-02-02 09:26:52
问题 I have the following struct: #[derive(Debug)] pub struct Entry { pub index: usize, pub name: String, pub filename_offset: u64, pub entry_type: EntryType, } #[derive(Debug)] pub enum EntryType { File { file_offset: u64, length: usize, }, Directory { parent_index: usize, next_index: usize, }, } Entry is an entry in a GameCube ROM file system table which describes a file or directory. I defined various methods for Entry such as Entry::read_filename and Entry::write_to_disk . However, I have some

Defining a method for a struct only when a field is a certain enum variant?

放肆的年华 提交于 2021-02-02 09:21:45
问题 I have the following struct: #[derive(Debug)] pub struct Entry { pub index: usize, pub name: String, pub filename_offset: u64, pub entry_type: EntryType, } #[derive(Debug)] pub enum EntryType { File { file_offset: u64, length: usize, }, Directory { parent_index: usize, next_index: usize, }, } Entry is an entry in a GameCube ROM file system table which describes a file or directory. I defined various methods for Entry such as Entry::read_filename and Entry::write_to_disk . However, I have some

Writing a generic function that takes an iterable container as parameter in Rust

笑着哭i 提交于 2021-02-02 07:34:01
问题 I want to write a generic function that takes any immutably borrowed iterable container such as an array, Vec , BTreeSet , etc. Since this function is part of a trait that I am implementing, I am not able to change the signature of it, so it's not possible to directly take an iterator as parameter and I also can't introduce any lifetime parameters to the function signature. Context I tried to implement the observer pattern in Rust. The observable and the observer look as follows: struct

Writing a generic function that takes an iterable container as parameter in Rust

喜你入骨 提交于 2021-02-02 07:32:35
问题 I want to write a generic function that takes any immutably borrowed iterable container such as an array, Vec , BTreeSet , etc. Since this function is part of a trait that I am implementing, I am not able to change the signature of it, so it's not possible to directly take an iterator as parameter and I also can't introduce any lifetime parameters to the function signature. Context I tried to implement the observer pattern in Rust. The observable and the observer look as follows: struct

Why does the compiler not complain that an iterator moved to a for loop is immutable?

為{幸葍}努か 提交于 2021-02-01 05:06:08
问题 I am reading the second edition of the Rust Book and I found the following sample in the iterators section: let v1 = vec![1, 2, 3]; let v1_iter = v1.iter(); for val in v1_iter { println!("Got: {}", val); } Why does the compiler not complain that v1_iter is immutable? The book says the for loop took ownership of v1_iter and made it mutable behind the scenes, but can you convert an immutable variable to mutable? 回答1: The book says the for loop took ownership of v1_iter and made it mutable

Why does the compiler not complain that an iterator moved to a for loop is immutable?

半世苍凉 提交于 2021-02-01 05:05:58
问题 I am reading the second edition of the Rust Book and I found the following sample in the iterators section: let v1 = vec![1, 2, 3]; let v1_iter = v1.iter(); for val in v1_iter { println!("Got: {}", val); } Why does the compiler not complain that v1_iter is immutable? The book says the for loop took ownership of v1_iter and made it mutable behind the scenes, but can you convert an immutable variable to mutable? 回答1: The book says the for loop took ownership of v1_iter and made it mutable

Rust编程进阶:046、通道介绍

一个人想着一个人 提交于 2021-01-31 00:27:25
1、Rust中一个实现消息传递并发的主要工具是通道。通道由两部分组成,一个是发送端,一个是接收端,发送端用来发送消息,接收端用来接收消息。发送者或者接收者任一被丢弃时就可以认为通道被关闭了。 2、通道介绍 (1)通过mpsc::channel,创建通道,mpsc是多个生产者,单个消费者; (2)通过spmc::channel,创建通道,spmc是一个生产者,多个消费者; (3)创建通道后返回的是发送者和消费者,示例: let (tx, rx) = mpsc::channel(); let (tx, rx) = spmc::channel(); 例子: use std::sync::mpsc; use std::thread; fn main() { let (tx, rx) = mpsc::channel(); // 创建通道 thread::spawn(move || { // 创建线程 let val = String::from("hi"); tx.send(val).unwrap(); // 发送val }); let received = rx.recv().unwrap(); // 接收val println!("Got: {}", received); } 知识点: 1、发送者的send方法返回的是一个Result<T, E>,如果接收端已经被丢弃了

Rust编程进阶:043、多线程介绍

十年热恋 提交于 2021-01-30 18:55:24
1、进程是资源分配的最小单位,线程是CPU调度的最小单位。 2、在使用多线程时,经常会遇到的一些问题: (1)竞争状态:多个线程以不一致的顺序访问数据或资源; (2)死锁:两个线程相互等待对方停止使用其所拥有的资源,造成两者都永久等待;A:1->2->3 B:2->1->3 t1:A:1,B:2 接下来:A:2,B:1 造成死锁 (3)只会发生在特定情况下且难以稳定重现和修复的bug 3、编程语言提供的线程叫做绿色线程,如go语言,在底层实现了M:N的模型,即M个绿色线程对应N个OS线程。但是,Rust标准库只提供1:1的线程模型的实现,即一个Rust线程对应一个OS线程。运行时代表二进制文件中包含的由语言本身提供的代码,这些代码根据语言的不同可大可小,不过非汇编语言都会有一定数量的运行时代码。通常,大家说一个语言“没有运行时”,是指这个语言的“运行时”很小。Rust、C都是几乎没有运行时的。 来源: oschina 链接: https://my.oschina.net/u/943779/blog/4938624