rust

Moving value out of function by dereferencing a reference [duplicate]

家住魔仙堡 提交于 2020-01-25 06:52:11
问题 This question already has answers here : Why is it legal to borrow a temporary? (3 answers) Why can I return a reference to a local literal but not a variable? (1 answer) Cannot move out of borrowed content / cannot move out of behind a shared reference (1 answer) Closed last month . Given this code: struct SimpleFoo {} fn create_simplefoo() -> SimpleFoo { let foo: &SimpleFoo = &SimpleFoo {}; *foo } pub fn main() { let foo = create_simplefoo(); } I get error[E0507]: cannot move out of `*foo`

How do I store a variable of type `impl Trait` in a struct?

笑着哭i 提交于 2020-01-25 03:09:26
问题 This works: let fut = Arc::new(Mutex::new(Box::pin(async { 1 }))); let mut conn_futures = BTreeMap::new(); // implicitly typed conn_futures.insert(123, fut); if let Some(fut) = conn_futures.get_mut(&123) { let fut = fut.clone(); self.pool.spawn(async move { let mut fut = fut.try_lock().unwrap(); (&mut *fut).await; }); }; How do I write the same thing inside a structure; what is the type of conn_futures ? According to the compiler, it's BTreeMap<i32, impl Future> , but there's no way to write

Why does this simple closure fail while the other two functions succeed?

送分小仙女□ 提交于 2020-01-25 00:19:45
问题 I've constructed a closure example that I can't get to work, nor can I find any reason why it shouldn't work. Why does it fail to compile on the last closure? Playground struct S {} fn filter<P>(predicate: P) where P: Fn(&S) -> bool, { predicate(&S {}); } fn main() { // this works filter(|_s| true); // this also works fn cb1(_s: &S) -> bool { true } filter(cb1); // but this doesn't work let cb2 = |_s| true; filter(cb2); } Output: error[E0631]: type mismatch in closure arguments --> /tmp

Explicitly stating the type of vector length for references

核能气质少年 提交于 2020-01-25 00:15:47
问题 I'm trying to get my head around Rust and I'm being faced with a probably obvious error. I have found a method which computes the dot product of two vectors and I want to implement it so that I do not need to consume the vectors to do so. Right now it looks like the following: pub fn dot(&u: Vec<f32>, &v: Vec<f32>) -> f32 { let len = cmp::min(u.len(), v.len()); let mut xs = &u[..len]; let mut ys = &v[..len]; let mut s = 0.; let (mut p0, mut p1, mut p2, mut p3, mut p4, mut p5, mut p6, mut p7)

Cannot assign to `self.x` because it is borrowed

喜夏-厌秋 提交于 2020-01-24 23:58:07
问题 I have 2 functions: // A simple struct struct S { w: u8, h: u8, x: Vec<u8>, y: Vec<u8>, } // Implementation of the struct S impl S { // Seems to work fn new(_w: u8, _h: u8, _x: &Vec<u8>, _y: &Vec<u8>) -> S { S { w: _w, h: _h, x: _x.clone(), y: _y.clone(), } } fn calc(&mut self) { let mut min_x = self.x.iter().min().unwrap(); let mut max_x = self.x.iter().max().unwrap(); let mut min_y = self.y.iter().min().unwrap(); let mut max_y = self.y.iter().max().unwrap(); // Here's the line that gives

How to call a C++ dynamic library from Rust?

强颜欢笑 提交于 2020-01-24 22:42:09
问题 I want to call a C++ dynamic library (*.so) from Rust, but I don't want to build it from Rust. Like this, cc::Build::new() .file("src/foo.cc") .shared_flag(true) .compile("libfoo.so"); In some cases, I only need to call several functions, not all the functions. How can I use it? 回答1: Before you go further, make sure you have a basic idea of Rust FFI (foreign function interface). In Rust, it's easy to call C, but hard to call C++. To call C functions in Rust, you just have to wrap them with

How to return an iterator over the keys of a HashMap from a trait implementation?

夙愿已清 提交于 2020-01-24 22:05:52
问题 I'm trying to build a simple graph library in Rust. There is a trait Graph that any graph must implement. This trait has only one function at the moment, nodes , which allows iteration of the graph's nodes using a for-in loop. An implementation of Graph , MapGraph , is a lightweight wrapper around a HashMap . MapGraph must implement the Graph trait method nodes . I'm having problems getting this to work. Here's the code for Graph : pub trait Graph<N> { fn nodes(&self) -> Box<dyn Iterator<Item

Why does solicit 0.4.4 attempt to use openssl 0.9.12 even though I have openssl 0.7.14 in my Cargo.toml?

被刻印的时光 ゝ 提交于 2020-01-24 20:48:05
问题 I have a simple project using the Solicit example to make a request with HTTPS. My Cargo.toml has: [package] name = "test" version = "0.1.0" authors = ["einchear"] [dependencies.openssl] version = "0.7.14" features = ["tlsv1_2", "npn"] [dependencies.solicit] version = "0.4.4" features = ["tls"] When I tried to run cargo build , the error is: error: Package `openssl v0.9.12` does not have these features: `npn, tlsv1_2` Why 0.9.12 instead 0.7.14? 回答1: Solicit hasn't released a new version in

How to get a compact but lossless string representation of a float in Rust?

两盒软妹~` 提交于 2020-01-24 20:20:33
问题 As a variation of How can I convert a float to string?, I'm looking for a simple way to obtain a string representation of a float that is both concise and lossless. For instance: let a = 1.0; let b = 1.1234567890123456789012345678901e50; let c = 1.1234567890123456789012345678901e-50; for x in &[a, b, c] { println!("{}", x); println!("{:?}", x); println!("{}", x.to_string()); println!("{}", f64::to_string(&x)); println!("{:e}", x); } This produces: 1 1.0 1 1 1e0

How can I specify the type of a closure argument?

天涯浪子 提交于 2020-01-24 19:25:28
问题 I'm trying to do something like this, but it fails to compile on inspect 's closure arguments: fn main() { vec!(1, 2, 3, 4) .windows(2) .inspect(|&&a[]| println!("{} {}", a[0], a[1])) .count(); } I've tried moving the slice name a around, but couldn't find the sweet spot of the compiler's understanding. 回答1: The direct answer is to use a colon, just like everywhere else you define an arguments type: fn main() { vec!(1, 2, 3, 4) .windows(2) .inspect(|a: &&[u8]| println!("{} {}", a[0], a[1]))