rust

Rust trait field lifetime

♀尐吖头ヾ 提交于 2020-06-01 04:48:26
问题 I think this is something obvious I'm missing, but here goes.. use std::io; pub trait Source<'a, T> { fn push(&self, t: T) -> io::Result<()>; fn link(&mut self, sink: &dyn Sink<'a, T>) -> io::Result<()>; } pub trait Sink<'a, T> { fn push(&self, t: T) -> io::Result<()>; fn link(&mut self, source: &dyn Source<T>) -> io::Result<()>; } pub struct SyncSource<'a, T> { sink: Option<&'a dyn Sink<'a, T>>, } impl<'a, T> SyncSource<'a, T> { pub fn new() -> SyncSource<'a, T> { SyncSource { sink: None, }

Is there a more ergonomic syntax for Either when using futures?

穿精又带淫゛_ 提交于 2020-06-01 02:28:53
问题 Here's an example of using Tokio to run a function that returns a future: use futures::sync::oneshot; use futures::Future; use std::thread; use std::time::Duration; use tokio; #[derive(Debug)] struct MyError { error_code: i32, } impl From<oneshot::Canceled> for MyError { fn from(_: oneshot::Canceled) -> MyError { MyError { error_code: 1 } } } fn deferred_task() -> impl Future<Item = i32, Error = MyError> { let (sx, rx) = oneshot::channel(); thread::spawn(move || { thread::sleep(Duration::from

How does assigning to a borrowed variable violate the rules of references?

别来无恙 提交于 2020-05-31 06:31:08
问题 I have this code: struct Foo<'a> { link: &'a i32, } fn main() { let mut x = 33; println!("x:{}", x); let ff = Foo { link: &x }; x = 22; } Which generates this compiler error: error[E0506]: cannot assign to `x` because it is borrowed --> src/main.rs:9:5 | 8 | let ff = Foo { link: &x }; | - borrow of `x` occurs here 9 | x = 22; | ^^^^^^ assignment to borrowed `x` occurs here The Rust book has only two rules: one or more references ( &T ) to a resource, exactly one mutable reference ( &mut T ).

`cannot infer an appropriate lifetime for autoref due to conflicting requirements` but can't change anything due to trait definition constraints

元气小坏坏 提交于 2020-05-31 04:57:12
问题 I was implementing linked lists by following along too many linked lists. When trying to implement iter_mut() , I did it myself and made the following code: type Link<T> = Option<Box<Node<T>>>; pub struct List<T> { head: Link<T>, } struct Node<T> { elem: T, next: Link<T>, } impl<T> List<T> { pub fn iter_mut(&mut self) -> IterMut<T> { IterMut::<T>(&mut self.head) } } pub struct IterMut<'a, T>(&'a mut Link<T>); impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next<'b>(&'b mut

How do I create a struct of references to traits when one object might implement multiple of the traits?

╄→гoц情女王★ 提交于 2020-05-31 04:13:12
问题 I have a struct which manages several sensors. I have a gyroscope, accelerometer, magnetometer, barometer, and thermometer. All of which are traits. pub struct SensorManager { barometer: Barometer + Sized, thermometer: Thermometer + Sized, gyroscope: Gyroscope + Sized, accelerometer: Accelerometer + Sized, magnetometer: Magnetometer + Sized } I need to make it modular so in the configuration file you can specify which sensors you are using. The problem is that some of the sensors overlap. For

why do we need to call take() for Option<T> variable

我只是一个虾纸丫 提交于 2020-05-30 07:50:06
问题 In this piece of code: pub struct Post { state: Option<Box<dyn State>>, content: String, } impl Post { pub fn new() -> Post { Post { state: Some(Box::new(Draft {})), content: String::new(), } } pub fn add_text(&mut self, text: &str) { self.content.push_str(text); } pub fn content(&self) -> &str { "" } pub fn request_review(&mut self) { if let Some(s) = self.state.take() { self.state = Some(s.request_review()) } } } trait State { fn request_review(self: Box<Self>) -> Box<dyn State>; } struct

Why can't the Rust compiler infer the type when calling parse? [duplicate]

坚强是说给别人听的谎言 提交于 2020-05-30 07:04:51
问题 This question already has answers here : Why do I get the error “the type of this value must be known in this context” when parsing a string to a number? (1 answer) Inferring types and using type annotations when parsing a string to a number (1 answer) Cannot infer type for `B` for filter_map().sum() (2 answers) Closed last year . I'm writing a sum calculator in Rust, but I found that the Rust compiler can't infer a type in some cases. Here is the code. fn main() { let mut s = String::new();

Why is are my published ports not working?

扶醉桌前 提交于 2020-05-30 06:33:44
问题 I've created a docker image containing a rust application that responds to get requests on port 8000. The application itself is a basic example using the rocket library (https://rocket.rs/) it looks like this #![feature(proc_macro_hygiene, decl_macro)] #[macro_use] extern crate rocket; #[get("/")] fn index() -> &'static str { "Hello, world!" } fn main() { rocket::ignite().mount("/", routes![index]).launch(); } I have compiled this and called it server I then created a Docker file to host it

Why is are my published ports not working?

为君一笑 提交于 2020-05-30 06:33:16
问题 I've created a docker image containing a rust application that responds to get requests on port 8000. The application itself is a basic example using the rocket library (https://rocket.rs/) it looks like this #![feature(proc_macro_hygiene, decl_macro)] #[macro_use] extern crate rocket; #[get("/")] fn index() -> &'static str { "Hello, world!" } fn main() { rocket::ignite().mount("/", routes![index]).launch(); } I have compiled this and called it server I then created a Docker file to host it

Can I pass params as an array?

99封情书 提交于 2020-05-29 15:43:50
问题 E.g., instead of assert_eq!(add(2,3), 5); is there some way to call something like let params: [u32; 2] = [2 ,3]; assert_eq!(call!(&add, params), 5); I would find this functionality very useful for testing. E.g., if I want to write multiple tests for a function that takes a large number of params, I want to be able to reuse a dummy param object like this: #[cfg(test)] mod tests { const dummy: [u32; 5] = [0, 0, 0, 0, 0]; #[test] fn test_first_param() { let mut params = dummy; params[0] = 1;