rust

Is it possible to pass an object method as argument to a function and bind it to the object?

余生长醉 提交于 2020-08-24 07:15:34
问题 Is it possible to make a bind to object method? For example, I have a vector and a lot of functions which do something if some item exists in the vector. I would implement it as follows: fn perform_if_exists(item: u8, vector: &Vec<u8>, func: fn(usize)) { let idx = vector.iter().position(|i| *i == item ); match idx { Some(i) => func(i), None => {}, } } fn main() { let v: Vec<u8> = vec![1, 2, 3]; perform_if_exists(1, &v, Vec<_>::remove); } but it gives a lot of errors. I think they are

Build HashSet from a vector in Rust

旧巷老猫 提交于 2020-08-22 02:36:31
问题 I want to build a HashSet<u8> from a Vec<u8> . I'd like to do this in one line of code, copying the data only once, using only 2n memory, but the only thing I can get to compile is this piece of .. junk, which I think copies the data twice and uses 3n memory. fn vec_to_set(vec: Vec<u8>) -> HashSet<u8> { let mut victim = vec.clone(); let x: HashSet<u8> = victim.drain(..).collect(); return x; } I was hoping to write something simple, like this: fn vec_to_set(vec: Vec<u8>) -> HashSet<u8> {

How to move tests into a separate file for binaries in Rust's Cargo?

家住魔仙堡 提交于 2020-08-21 09:52:15
问题 I created a new binary using Cargo: cargo new my_binary --bin A function in my_binary/src/main.rs can be used for a test: fn function_from_main() { println!("Test OK"); } #[test] fn my_test() { function_from_main(); } And cargo test -- --nocapture runs the test as expected. What's the most straightforward way to move this test into a separate file, (keeping function_from_main in my_binary/src/main.rs )? I tried to do this but am not sure how to make my_test call function_from_main from a

How to move tests into a separate file for binaries in Rust's Cargo?

*爱你&永不变心* 提交于 2020-08-21 09:47:52
问题 I created a new binary using Cargo: cargo new my_binary --bin A function in my_binary/src/main.rs can be used for a test: fn function_from_main() { println!("Test OK"); } #[test] fn my_test() { function_from_main(); } And cargo test -- --nocapture runs the test as expected. What's the most straightforward way to move this test into a separate file, (keeping function_from_main in my_binary/src/main.rs )? I tried to do this but am not sure how to make my_test call function_from_main from a

How to wait for a list of async function calls in rust?

喜你入骨 提交于 2020-08-20 12:01:31
问题 I have a list of async functions in rust that I want to execute concurrently and then wait for all them to finish. The working code I have right now is async fn start_consumers(&self) { for consumer in &self.consumers { consumer.consume().await; } } This is not quite accurate as the functions are executed serially. I am looking for something like join! , but which works on a dynamic vector, Using which I should be able to write something like async fn start_consumers(&self) { let mut v = Vec:

How do I recursively pass a mutable reference?

£可爱£侵袭症+ 提交于 2020-08-20 08:54:45
问题 I am attempting to solve this problem in Rust. Here is my non-compiling Rust code: use std::collections::HashMap; fn main() { // initialize HashMap let mut fibs: HashMap<u32, u32> = HashMap::new(); fibs.insert(0, 1); fibs.insert(1, 1); let mut n = 1; let mut sum = 0; while fib(n, &mut fibs) < 4000000 { sum += if fib(n, &mut fibs) % 2 == 0 { fib(n, &mut fibs) } else { 0 }; n += 1; } println!("{}", sum); } fn fib(n: u32, fibs: &mut HashMap<u32, u32>) -> u32 { if !fibs.contains_key(&n) { fibs

What does it mean when “method exists but trait bounds not satisfied”?

丶灬走出姿态 提交于 2020-08-20 08:24:11
问题 I'm new to Rust and observed something that I couldn't reason against. When I write fn main() { ('a'..'z').all(|_| true); } The compiler reports an error: error[E0599]: no method named `all` found for type `std::ops::Range<char>` in the current scope --> src/main.rs:2:16 | 2 | ('a'..'z').all(|_| true) | ^^^ | = note: the method `all` exists but the following trait bounds were not satisfied: `std::ops::Range<char> : std::iter::Iterator` When I change it to fn main() { (b'a'..b'z').all(|_| true

Rustlings thread exercise, why do I NOT dereference Mutex(Struct)?

六月ゝ 毕业季﹏ 提交于 2020-08-20 05:11:07
问题 I'm learning Rust and have no experience with threads. I'm going through the Rustlings course and I've solved the threads1.rs exercise, but I don't understand why my Mutex struct doesn't need to be dereferenced. use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; struct JobStatus { jobs_completed: u32, } fn main() { let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 })); let status_shared = Arc::clone(&status); thread::spawn(move || { for _ in 0..10 { thread:

科技爱好者周刊(第 115 期):保护你的 DNA,不要泄漏

99封情书 提交于 2020-08-20 02:13:34
这里记录每周值得分享的科技内容,周五发布。 本杂志开源(GitHub: ruanyf/weekly ),欢迎提交 issue,投稿或推荐你的项目。 周刊讨论区的帖子 《谁在招人?》 ,提供大量就业信息,欢迎访问或发布工作/实习岗位。 封面图 澳门去年11月举办龙马巡游,该大型机械装置为中法团队合作打造,上身为龙,下身为马,身体各部分都能动,还能喷火。(出处: Instagram ) 本周话题:保护你的 DNA,不要泄漏 上周,美国佛罗里达州通过了 《DNA 隐私法》 ,成为美国第一个对 DNA 立法的州。它明文规定 DNA 属于个人隐私,保险公司不得获取 DNA 测试的结果。 为什么 DNA 不能让保险公司知道? 因为 DNA 包含了一个人所有的遗传信息,你有什么基因缺陷,一查 DNA 都能知道。保险公司拿到你的 DNA 以后,就能识别你可能会得哪些疾病,于是相应调整费率,或拒绝你投保人寿险和医疗险。 DNA 不仅对保险公司有用,对就业和招生也有用,可以用来找出基因上最合适的人选。所以,DNA 里面的个人信息可能不利于你,要注意保护,防止泄漏。 DNA 测序已经是一项常规技术了,普通实验室都能做,毫无难度。现在,新生儿出生前,医院都会建议孕妇做一下产前 DNA 检查,防止遗传病。2018年,湖南一个孕妇 起诉华大基因公司 ,原因就是该公司的 DNA 检查一切正常

Rust regex pattern - unrecognized escape pattern

孤街浪徒 提交于 2020-08-19 16:57:46
问题 I do have following string: \"lengthSeconds\":\"2664\" which I would like to match with this regexp: Regex::new("lengthSeconds\\\":\\\"(\\d+)\\\"") I even tried this: Regex::new(r#"lengthSeconds\":\"(\d+)\""#) but I'm getting this: regex parse error: lengthSeconds\":\"(\d+)\" ^^ error: unrecognized escape sequence What's wrong with the regexp pattern? 回答1: By using r#..#, you treat your string as a raw string and hence do not process any escapes. However, since backslashes are special