rust

Rust regex pattern - unrecognized escape pattern

不羁的心 提交于 2020-08-19 16:57:35
问题 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

How to add external packages and run in rust compiler?

跟風遠走 提交于 2020-08-19 16:55:25
问题 I am compiling and building an example program using rust. I chose rustc instead of cargo for compiling because it being a simple personal test project. So far using rustc for compiling and building executable worked fine but when I tried to add an external rand package its giving me this error 1 | extern crate rand; | ^^^^^^^^^^^^^^^^^^ can't find crate This is the full code extern crate rand; use rand::Rng; fn main() { for x in 1..11 { let random_number = rand::thread_rng() .gen_range(1,

Why do I get an error that “Sync is not satisfied” when moving self, which contains an Arc, into a new thread?

匆匆过客 提交于 2020-08-19 16:55:07
问题 I have a struct which holds an Arc<Receiver<f32>> and I'm trying to add a method which takes ownership of self , and moves the ownership into a new thread and starts it. However, I'm getting the error error[E0277]: the trait bound `std::sync::mpsc::Receiver<f32>: std::marker::Sync` is not satisfied --> src/main.rs:19:9 | 19 | thread::spawn(move || { | ^^^^^^^^^^^^^ `std::sync::mpsc::Receiver<f32>` cannot be shared between threads safely | = help: the trait `std::marker::Sync` is not

How to add external packages and run in rust compiler?

你。 提交于 2020-08-19 16:52:35
问题 I am compiling and building an example program using rust. I chose rustc instead of cargo for compiling because it being a simple personal test project. So far using rustc for compiling and building executable worked fine but when I tried to add an external rand package its giving me this error 1 | extern crate rand; | ^^^^^^^^^^^^^^^^^^ can't find crate This is the full code extern crate rand; use rand::Rng; fn main() { for x in 1..11 { let random_number = rand::thread_rng() .gen_range(1,

Why does the order of borrowing matter in rust?

心不动则不痛 提交于 2020-08-19 11:51:22
问题 fn say_hello(s: &str) { println!("Hello {}", s); } Why does this work fn main() { let mut name = String::from("Charlie"); let x = &mut name; say_hello(x); name.push_str(" Brown"); } but this doesn't? fn main() { let mut name = String::from("Charlie"); let x = &mut name; name.push_str(" Brown"); say_hello(x); } all I did was switch the order of the two functions but it seems like x has mutably borrowed name and push_str has also mutably borrowed name in both situations, so why does the first

Difference between iter() and into_iter() on a shared, borrowed Vec?

依然范特西╮ 提交于 2020-08-19 11:41:45
问题 I am reading the Rust 101 tutorial, where the author talks about shared borrowing with the example of a Vec object passed to a function. Below is a slightly adapted MWE of what the the tutorial is teaching. The interesting part is v.iter() in vec_min . The author writes: This time, we explicitly request an iterator for the vector v . The method iter borrows the vector it works on, and provides shared borrows of the elements. But what happens if I use a for ... in ... construction on an object

Why does the order of borrowing matter in rust?

别等时光非礼了梦想. 提交于 2020-08-19 11:41:39
问题 fn say_hello(s: &str) { println!("Hello {}", s); } Why does this work fn main() { let mut name = String::from("Charlie"); let x = &mut name; say_hello(x); name.push_str(" Brown"); } but this doesn't? fn main() { let mut name = String::from("Charlie"); let x = &mut name; name.push_str(" Brown"); say_hello(x); } all I did was switch the order of the two functions but it seems like x has mutably borrowed name and push_str has also mutably borrowed name in both situations, so why does the first

Why does the order of borrowing matter in rust?

这一生的挚爱 提交于 2020-08-19 11:41:09
问题 fn say_hello(s: &str) { println!("Hello {}", s); } Why does this work fn main() { let mut name = String::from("Charlie"); let x = &mut name; say_hello(x); name.push_str(" Brown"); } but this doesn't? fn main() { let mut name = String::from("Charlie"); let x = &mut name; name.push_str(" Brown"); say_hello(x); } all I did was switch the order of the two functions but it seems like x has mutably borrowed name and push_str has also mutably borrowed name in both situations, so why does the first

How to implement Iterator yielding mutable references

倖福魔咒の 提交于 2020-08-19 07:43:10
问题 I am trying to implement a simple lookup iterator: pub struct LookupIterMut<'a, D> { data : &'a mut [D], indices : &'a [usize], i: usize } impl<'a, D> Iterator for LookupIterMut<'a, D> { type Item = &'a mut D; fn next(&mut self) -> Option<Self::Item> { if self.i >= self.indices.len() { None } else { let index = self.indices[self.i] as usize; self.i += 1; Some(&mut self.data[index]) // error here } } } The idea was to allow a caller consecutive mutable access to an internal storage. However I

What is the correct way to read a binary file in chunks of a fixed size and store all of those chunks into a Vec?

寵の児 提交于 2020-08-19 06:24:32
问题 I'm having trouble with opening a file. Most examples read files into a String or read the entire file into a Vec . What I need is to read a file into chunks of a fixed size and store those chunks into an array ( Vec ) of chunks. For example, I have a file called my_file of exactly 64 KB size and I want to read it in chunks of 16KB so I would end up with an Vec of size 4 where each element is another Vec with size 16Kb (0x4000 bytes). After reading the docs and checking other Stack Overflow