rust

How do I reuse code for similar yet distinct types in Rust?

喜欢而已 提交于 2020-01-16 20:19:13
问题 I have a basic type with some functionality, including trait implementations: use std::fmt; use std::str::FromStr; pub struct MyIdentifier { value: String, } impl fmt::Display for MyIdentifier { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.value) } } impl FromStr for MyIdentifier { type Err = (); fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(MyIdentifier { value: s.to_string(), }) } } This is a simplified example, real code would be more complex. I want

Rust - Pass a function reference to threads

断了今生、忘了曾经 提交于 2020-01-16 19:35:48
问题 Say I have a struct like: pub struct MyStruct { f: Arc<dyn Fn(Vec<f64>) -> Vec<f64>>, } impl MyStruct { pub fn new(f: Arc<dyn Fn(Vec<f64>) -> Vec<f64>>) -> MyStruct { MyStruct { f } } pub fn start(&self) { for _ in 0..5 { let f = self.f.clone(); thread::spawn(move || { let v: Vec<f64> = get_random_vector(); let v = (f)(v); // do something with v }); } } } I'm getting an error that the function cannot be shared between threads safely since the dyn Fn(Vec<f64>) -> Vec<f64>) type doesn't

Multiplying elements from two different arrays [duplicate]

☆樱花仙子☆ 提交于 2020-01-16 18:28:42
问题 This question already has an answer here : How do I compute the dot product of two Rust arrays / slices / vectors? (1 answer) Closed 6 months ago . I'm trying to multiply each element from two different arrays. I mean, I have array1 = [i1 , i2] and array2 = [j1, j2] so I need to do (i1 * j1) + (i2 * j2) . How can I approach this in Rust? I've been researching in The Book and saw some methods that possibly could help: map and fold . But I'm a bit lost. Thanks in advance! fn sum_product(a: [f32

Struct members who are traits that use associated types

北慕城南 提交于 2020-01-16 16:31:28
问题 I have a follow up question to this question: Expose a HashMap in a generic way that disregards the HashMap value Suppose I want to use HashMapContainer (the same one that was defined in the previous question's first answer) as a member in another struct (let's call it MyDB ) and in MyDB constructor I want to decide whether to construct this member as HashMapContainerImpl1 or HashMapContainerImpl2 . I don't want to define MyDB as a template (e.g MyDB<T> ) because MyDB users don't care about

Struct members who are traits that use associated types

你说的曾经没有我的故事 提交于 2020-01-16 16:31:12
问题 I have a follow up question to this question: Expose a HashMap in a generic way that disregards the HashMap value Suppose I want to use HashMapContainer (the same one that was defined in the previous question's first answer) as a member in another struct (let's call it MyDB ) and in MyDB constructor I want to decide whether to construct this member as HashMapContainerImpl1 or HashMapContainerImpl2 . I don't want to define MyDB as a template (e.g MyDB<T> ) because MyDB users don't care about

How do I write a rust function that can both read and write to a cache?

时光毁灭记忆、已成空白 提交于 2020-01-16 14:39:48
问题 Original Problem Statement I'm trying to write a function that can both read and write from a cache, but I'm running into a problem where the compiler says I can't both mutably and immutably borrow the cache. I've read through https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html , https://naftuli.wtf/2019/03/20/rust-the-hard-parts/ and random stack overflow/Reddit posts, but I can't see how to apply what they say to this code. use std::collections::HashMap; struct

Rust - cannot infer an appropriate lifetime for autoref due to conflicting requirements [duplicate]

落花浮王杯 提交于 2020-01-16 14:35:27
问题 This question already has answers here : Cannot infer an appropriate lifetime for autoref due to conflicting requirements (1 answer) Why can't I store a value and a reference to that value in the same struct? (2 answers) Closed 8 months ago . I'm new to Rust's lifetimes concept and can't wrap my head around a particular piece of code. I've been working with Rust's SDL bindings and its Texture/TextureCreator classes and the problem basically boils down to the following scheme: struct Creator;

Function pointers in Rust using constrained generics

荒凉一梦 提交于 2020-01-16 09:34:22
问题 I'm trying to create a struct that looks like this: struct MediaLibrary<B> where B: Ord, { root_dir: PathBuf, item_meta_fn: String, self_meta_fn: String, media_item_filter: fn(&Path) -> bool, media_item_sort_key: fn(&Path) -> B, } The last two fields are meant to be used as a predicate to test if a given path is a valid media file and to sort a vector of paths (using sort_by_key ), respectively. However, as it is right now, the design is inflexible: both functions are fixed to accept only

Function pointers in Rust using constrained generics

孤人 提交于 2020-01-16 09:32:51
问题 I'm trying to create a struct that looks like this: struct MediaLibrary<B> where B: Ord, { root_dir: PathBuf, item_meta_fn: String, self_meta_fn: String, media_item_filter: fn(&Path) -> bool, media_item_sort_key: fn(&Path) -> B, } The last two fields are meant to be used as a predicate to test if a given path is a valid media file and to sort a vector of paths (using sort_by_key ), respectively. However, as it is right now, the design is inflexible: both functions are fixed to accept only

How to wrap existing C functions with Rust or how to call C functions from Rust?

送分小仙女□ 提交于 2020-01-16 08:43:13
问题 I have existing C code and its header and I need to call the C code from Rust. I tried it many ways and referred to documents but I didn't understand how to apply that to my code. I'm facing difficulties converting C functions into Rust. Please help me with some examples so that I can understand easily. I tried to use the examples given in the Rust book and other website examples, but no resource has more details on this. C_code.h void ifx_vec_init_r(ifx_Vector_R_t* vector, ifx_Float_t* d,