rust

Using Rust dereferencing operators &* vs * with Self?

不想你离开。 提交于 2021-02-04 20:51:11
问题 I want to write an LRU Cache with a memory size limitation rather than the "number of objects" limitation in std. After trying to figure it out for myself, I cheated and looked at an existing implementation, and I almost understand it, but this stops me: struct KeyRef<K> { k: *const K, } impl<K: Hash> Hash for LruKeyRef<K> { fn hash<H: Hasher>(&self, state: &mut H) { unsafe { (*self.k).hash(state) } } } impl<K: PartialEq> PartialEq for LruKeyRef<K> { fn eq(&self, other: &LruKeyRef<K>) -> bool

Is there any way to get unstable features on the compiler versions in stable or beta?

…衆ロ難τιáo~ 提交于 2021-02-04 19:34:05
问题 I'm in a bit of a bind right now. My personal code relies on rand , which is currently not compiling on Nightly 1.7, but does work on Beta 1.6 and stable. However, my work also uses unstable features such as box syntax/patterns/raw and convert in a way that can't (easily) be refactored out. Is there any way, including by compiling from source, to get Rust 1.6 "as if" it were a nightly? I'm on Windows (10)/MSYS 2 if that complicates things any, such as building the source. 回答1: You cannot

Does type constructor implement Fn?

荒凉一梦 提交于 2021-02-04 18:15:30
问题 I am not sure that the title of my question is correct since I am not sure where exactly placed. Let say I have a code which looks like: struct MyWrapper(u64); fn my_func<F>(f: F, n: u64) -> MyWrapper where F: Fn(u64) -> MyWrapper, { f(n) } fn main() { my_func(MyWrapper, 3); } It compiles and works so it looks like MyWrapper implements trait Fn . However, should I try to use it in a trait. struct MyWrapper(u64); trait MyTrait where Self: Fn(u64) -> MyWrapper, { } impl MyTrait for MyWrapper{}

Why is there a borrow of a moved value when calling a method that takes self by value with an argument that also calls a method?

断了今生、忘了曾经 提交于 2021-02-04 17:47:20
问题 I ran into an issue that forces me to split a nice oneliner into a {} block with an intermediate let . The reason for this isn't clear to me at all. I was able to isolate the issue in this minimal example: struct AB { a: u8, b: u8, } impl AB { fn foo(&self) -> String { String::from("foo") } fn bar(self, x: String) -> String { format!("{} - {} - {}!", x, self.a, self.b) } } fn main() { let x = AB { a: 3, b: 5 }; let result = x.bar(x.foo()); println!("{}", result); } I was under the impression

Given two absolute paths, how can I express one of the paths relative to the other?

好久不见. 提交于 2021-02-04 17:38:07
问题 I think this should be quite doable, given that there is a nice function canonicalize which normalizes paths (so I can start by normalizing my two input paths) and Path and PathBuf give us a way of iterating over the parts of paths through components. I imagine something could be worked out here to factor out a common prefix, then prepend as many .. components as remain in the anchor path to what remains of the initial input path. My problem seems to be pretty common: How to find relative

Why do I get the error “there is no reactor running, must be called from the context of Tokio runtime” even though I have #[tokio::main]?

為{幸葍}努か 提交于 2021-02-04 17:18:22
问题 I'm following the mdns Rust documentation and pasted the example code but it throws the following error: thread 'main' panicked at 'there is no reactor running, must be called from the context of Tokio runtime' Here's the code that I have: use futures_util::{pin_mut, stream::StreamExt}; use mdns::{Error, Record, RecordKind}; use std::{net::IpAddr, time::Duration}; const SERVICE_NAME: &'static str = "_googlecast._tcp.local"; #[tokio::main] async fn main() -> Result<(), Error> { // Iterate

Clone an Rc<RefCell<MyType> trait object and cast it

杀马特。学长 韩版系。学妹 提交于 2021-02-04 16:42:18
问题 This question is related to Rust: Clone and Cast Rc pointer Let's say I have this piece of code which works fine: use std::rc::Rc; trait TraitAB : TraitA + TraitB { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA>; fn as_b(self: Rc<Self>) -> Rc<dyn TraitB>; } trait TraitA {} trait TraitB {} struct MyType {} impl TraitAB for MyType { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA> {self} fn as_b(self: Rc<Self>) -> Rc<dyn TraitB> {self} } impl TraitA for MyType {} impl TraitB for MyType {} fn main() { let a

Clone an Rc<RefCell<MyType> trait object and cast it

感情迁移 提交于 2021-02-04 16:42:12
问题 This question is related to Rust: Clone and Cast Rc pointer Let's say I have this piece of code which works fine: use std::rc::Rc; trait TraitAB : TraitA + TraitB { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA>; fn as_b(self: Rc<Self>) -> Rc<dyn TraitB>; } trait TraitA {} trait TraitB {} struct MyType {} impl TraitAB for MyType { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA> {self} fn as_b(self: Rc<Self>) -> Rc<dyn TraitB> {self} } impl TraitA for MyType {} impl TraitB for MyType {} fn main() { let a

Clone an Rc<RefCell<MyType> trait object and cast it

喜欢而已 提交于 2021-02-04 16:42:05
问题 This question is related to Rust: Clone and Cast Rc pointer Let's say I have this piece of code which works fine: use std::rc::Rc; trait TraitAB : TraitA + TraitB { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA>; fn as_b(self: Rc<Self>) -> Rc<dyn TraitB>; } trait TraitA {} trait TraitB {} struct MyType {} impl TraitAB for MyType { fn as_a(self: Rc<Self>) -> Rc<dyn TraitA> {self} fn as_b(self: Rc<Self>) -> Rc<dyn TraitB> {self} } impl TraitA for MyType {} impl TraitB for MyType {} fn main() { let a

Is it possible to combine two patterns, one with a match guard, in the same match arm?

心不动则不痛 提交于 2021-02-04 14:42:37
问题 I want to check if a string contains '$' and if there is something after the '$': I tried this code: fn test(s: String) { match s.find('$') { None | (Some(pos) if pos == s.len() - 1) => { expr1(); } _ => { expr2(); } } } But it doesn't compile: error: expected one of `)` or `,`, found `if` Is it impossible to combine None and Some in one match-arm? If so, is there a simple way to not duplicate expr1() except moving it into a separate function? 回答1: It is impossible to have the match-guard