rust

Using a HashSet to canonicalize objects in Rust

99封情书 提交于 2020-03-16 06:27:48
问题 As an educational exercise, I'm looking at porting cvs-fast-export to Rust. Its basic mode of operation is to parse a number of CVS master files into a intermediate form, and then to analyse the intermediate form with the goal of transforming it into a git fast-export stream. One of the things that is done when parsing is to convert common parts of the intermediate form into a canonical representation. A motivating example is commit authors. A CVS repository may have hundreds of thousands of

String's lifetime when returning Vec<&str> [duplicate]

瘦欲@ 提交于 2020-03-16 06:14:09
问题 This question already has answers here : Return local String as a slice (&str) (4 answers) Closed 4 years ago . Simple code: fn foo() -> Vec<&'static str> { let mut vec = Vec::new(); let mut string = String::new(); // doing something with string... vec.push(string.as_str()); return vector; // error here: string doesn't live long enough } I have problem that I need to process with string and return it in Vec as str. Problem is that binding string doesn't live long enough, since it goes out of

String's lifetime when returning Vec<&str> [duplicate]

前提是你 提交于 2020-03-16 06:13:04
问题 This question already has answers here : Return local String as a slice (&str) (4 answers) Closed 4 years ago . Simple code: fn foo() -> Vec<&'static str> { let mut vec = Vec::new(); let mut string = String::new(); // doing something with string... vec.push(string.as_str()); return vector; // error here: string doesn't live long enough } I have problem that I need to process with string and return it in Vec as str. Problem is that binding string doesn't live long enough, since it goes out of

Is there a simple way to mutate an enum field in Rust?

白昼怎懂夜的黑 提交于 2020-03-16 06:05:04
问题 Suppose we have an enum that looks like this: enum MyEnum { Field1, Field2 {x: f64, y: f64}, /* Maybe some other fields */ MyString(String), } Now I created an instance of this enum of the subtype MyString and after some actions I want to mutate it. For example: fn main() { let mut my_enum = MyEnum::MyString("Hello, world".to_string()); /* Some actions */ // Mutating the string match my_enum { MyEnum::MyString(ref mut content) => { content.push('!'); }, _ => {} } // Printing the string match

Is there a simple way to mutate an enum field in Rust?

若如初见. 提交于 2020-03-16 06:02:13
问题 Suppose we have an enum that looks like this: enum MyEnum { Field1, Field2 {x: f64, y: f64}, /* Maybe some other fields */ MyString(String), } Now I created an instance of this enum of the subtype MyString and after some actions I want to mutate it. For example: fn main() { let mut my_enum = MyEnum::MyString("Hello, world".to_string()); /* Some actions */ // Mutating the string match my_enum { MyEnum::MyString(ref mut content) => { content.push('!'); }, _ => {} } // Printing the string match

Check if length of all vectors is the same in Rust

妖精的绣舞 提交于 2020-03-16 05:58:06
问题 Given a vector of vectors of some value T , ie. Vec<Vec<T>> . What's the idiomatic way to check if the inner vectors have the same length? (without external dependencies) That is, true if all the inner vectors have the same length, and false otherwise. 回答1: You can use the all method to check if all elements of an iterator match a predicate. Then just compare against the first element in the list. fn main() { let vec_of_vecs = vec![ vec![1, 2, 3], vec![1, 2, 3], vec![1, 2, 3], vec![1, 2, 3],

How to pass a method as callback

夙愿已清 提交于 2020-03-15 05:51:02
问题 In Python or C++, a class say A can delegate some work to another instance of class Say B, and set a callback method of A in B. I try to do this in Rust, but so far I got nowhere, beaten by Rust compiler. Here is Code I have tried, remaining code is at the end of this post. In A::test I tried using closure to get a Fn() trait object as callback. // let b = B::new(self.finish)); // ideally but would not compile // let test :Box<Fn(String)> = Box::new(move |msg| {self.finish(msg);}); // cannot

How to pass a method as callback

痞子三分冷 提交于 2020-03-15 05:47:15
问题 In Python or C++, a class say A can delegate some work to another instance of class Say B, and set a callback method of A in B. I try to do this in Rust, but so far I got nowhere, beaten by Rust compiler. Here is Code I have tried, remaining code is at the end of this post. In A::test I tried using closure to get a Fn() trait object as callback. // let b = B::new(self.finish)); // ideally but would not compile // let test :Box<Fn(String)> = Box::new(move |msg| {self.finish(msg);}); // cannot

What is the concrete type of a future returned from `async fn`?

巧了我就是萌 提交于 2020-03-15 05:44:08
问题 What type should I use for a vector that stores futures? I tried to make multiple concurrent requests on the same URL and save all the futures into the vector to use with join_all . If I don't set a type for the vector explicitly, everything works. I understand that Rust can find the proper type of a variable. CLion determines the vector type as Vec<dyn Future<Output = ()>> , but when I try to set the type by myself, it gives me an error: error[E0277]: the size for values of type `dyn core:

What is the concrete type of a future returned from `async fn`?

允我心安 提交于 2020-03-15 05:43:47
问题 What type should I use for a vector that stores futures? I tried to make multiple concurrent requests on the same URL and save all the futures into the vector to use with join_all . If I don't set a type for the vector explicitly, everything works. I understand that Rust can find the proper type of a variable. CLion determines the vector type as Vec<dyn Future<Output = ()>> , but when I try to set the type by myself, it gives me an error: error[E0277]: the size for values of type `dyn core: