rust

Struct with a generic trait which is also a generic trait

↘锁芯ラ 提交于 2020-11-29 08:51:48
问题 In Rust 1.15, I have created a trait to abstract over reading & parsing file format(s). I'm trying to create a struct which has this generic trait inside. I have this trait: use std::io::Read; trait MyReader<R: Read> { fn new(R) -> Self; fn into_inner(self) -> R; fn get_next(&mut self) -> Option<u32>; fn do_thingie(&mut self); } I want to make a struct which has a reference to something that implements this. struct MyIterThing<'a, T: MyReader<R>+'a> { inner: &'a mut T, } Gives the following

How to write a generic function taking any iterator of `u32` or `&u32`?

我怕爱的太早我们不能终老 提交于 2020-11-29 08:22:46
问题 I'm trying to write a function that processes a sequence of integers. fn process_one(n: u32) {} fn process<II>(ii: II) where II: IntoIterator<Item = u32>, { for n in ii { process_one(n); } } I want the client to be able to pass a Vec<u32> without consuming it ( process(&v) ). This function can't be used because <&Vec<u32> as IntoIterator>::Item is &u32 ; I'd have to pass v.iter().cloned() instead, which is annoying. Alternatively, I could make the bound Item = &u32 and use process_one(*n) ,

How to write a generic function taking any iterator of `u32` or `&u32`?

女生的网名这么多〃 提交于 2020-11-29 08:21:11
问题 I'm trying to write a function that processes a sequence of integers. fn process_one(n: u32) {} fn process<II>(ii: II) where II: IntoIterator<Item = u32>, { for n in ii { process_one(n); } } I want the client to be able to pass a Vec<u32> without consuming it ( process(&v) ). This function can't be used because <&Vec<u32> as IntoIterator>::Item is &u32 ; I'd have to pass v.iter().cloned() instead, which is annoying. Alternatively, I could make the bound Item = &u32 and use process_one(*n) ,

How to write a generic function taking any iterator of `u32` or `&u32`?

Deadly 提交于 2020-11-29 08:20:42
问题 I'm trying to write a function that processes a sequence of integers. fn process_one(n: u32) {} fn process<II>(ii: II) where II: IntoIterator<Item = u32>, { for n in ii { process_one(n); } } I want the client to be able to pass a Vec<u32> without consuming it ( process(&v) ). This function can't be used because <&Vec<u32> as IntoIterator>::Item is &u32 ; I'd have to pass v.iter().cloned() instead, which is annoying. Alternatively, I could make the bound Item = &u32 and use process_one(*n) ,

How to write a generic function taking any iterator of `u32` or `&u32`?

泄露秘密 提交于 2020-11-29 08:20:26
问题 I'm trying to write a function that processes a sequence of integers. fn process_one(n: u32) {} fn process<II>(ii: II) where II: IntoIterator<Item = u32>, { for n in ii { process_one(n); } } I want the client to be able to pass a Vec<u32> without consuming it ( process(&v) ). This function can't be used because <&Vec<u32> as IntoIterator>::Item is &u32 ; I'd have to pass v.iter().cloned() instead, which is annoying. Alternatively, I could make the bound Item = &u32 and use process_one(*n) ,

How can I specify a custom Cargo output directory?

耗尽温柔 提交于 2020-11-29 05:31:38
问题 I put this in my Cargo.toml [build] target-dir = "../my-target" However, Cargo doesn't recognize this key. cargo run --release --bin my_project warning: unused manifest key: build error: failed to open: /.../project-root/target/releases/.cargo-lock Caused by: Permission denied (os error 13) The custom target dir with the environment variable works: CARGO_TARGET_DIR=../my-target cargo run --bin my_project but how can I specify '../my-target' in Cargo.toml? 回答1: [build] is a Cargo-level

How do I idiomatically convert a bool to an Option or Result in Rust?

一曲冷凌霜 提交于 2020-11-29 04:21:03
问题 It seems there is no way of such one-line conversion using std . I do not like this kind of verbosity: match my_bool { true => Ok(()), false => Err(MyError::False), } I would like to use some kind of one-liner, e.g.: let my_bool = true; let my_option = my_bool.to_option(MyObject{}); // true => MyObject{}, false => None let my_result = my_bool.to_result(MyObject{}, MyError{}); // true => MyObject{}, false => MyError{} What is the shortest piece of code doing that? 回答1: There is the boolinator

How do I idiomatically convert a bool to an Option or Result in Rust?

不羁岁月 提交于 2020-11-29 04:19:26
问题 It seems there is no way of such one-line conversion using std . I do not like this kind of verbosity: match my_bool { true => Ok(()), false => Err(MyError::False), } I would like to use some kind of one-liner, e.g.: let my_bool = true; let my_option = my_bool.to_option(MyObject{}); // true => MyObject{}, false => None let my_result = my_bool.to_result(MyObject{}, MyError{}); // true => MyObject{}, false => MyError{} What is the shortest piece of code doing that? 回答1: There is the boolinator

Using enums for dynamic polymorphism in Rust

亡梦爱人 提交于 2020-11-28 08:29:46
问题 When one already knows all the finite number of types involved in some code which needs dynamic polymorphism, using enum can be better for performances compared to using Box since the latter uses dynamic memory allocation and you'll need to use trait objects which have virtual function call as well. That said, compared to the equivalent code in C++ using std::variant and std::visit , looks like Rust in this scenario has more boilerplate coding involved, at least for me (I have not yet learned

Using enums for dynamic polymorphism in Rust

瘦欲@ 提交于 2020-11-28 08:28:10
问题 When one already knows all the finite number of types involved in some code which needs dynamic polymorphism, using enum can be better for performances compared to using Box since the latter uses dynamic memory allocation and you'll need to use trait objects which have virtual function call as well. That said, compared to the equivalent code in C++ using std::variant and std::visit , looks like Rust in this scenario has more boilerplate coding involved, at least for me (I have not yet learned