rust

How to query a child process status regularly

与世无争的帅哥 提交于 2021-01-28 07:38:16
问题 I have spawned a child process using Rust's Command API. Now, I need to watch this process for a few seconds before moving on because the process may die early. On success, it should run "forever", so I can't just wait. There's a nightly feature called try_wait which does what I want, but I really don't think I should run Rust nightly just for this! I think I could start a new thread and keep it waiting forever or until the process dies... but I would like to not hang my process with that

How to properly pass Iterators to a function in Rust

别来无恙 提交于 2021-01-28 07:30:31
问题 I want to pass Iterators to a function, which then computes some value from these iterators. I am not sure how a robust signature to such a function would look like. Lets say I want to iterate f64. You can find the code in the playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c614429c541f337adb102c14518cf39e My first attempt was fn dot(a : impl std::iter::Iterator<Item = f64>,b : impl std::iter::Iterator<Item = f64>) -> f64 { a.zip(b).map(|(x,y)| x*y).sum() }

Why doesn't Rust support trait object upcasting?

空扰寡人 提交于 2021-01-28 05:36:17
问题 Given this code: trait Base { fn a(&self); fn b(&self); fn c(&self); fn d(&self); } trait Derived : Base { fn e(&self); fn f(&self); fn g(&self); } struct S; impl Derived for S { fn e(&self) {} fn f(&self) {} fn g(&self) {} } impl Base for S { fn a(&self) {} fn b(&self) {} fn c(&self) {} fn d(&self) {} } Unfortunately, I cannot cast &Derived to &Base : fn example(v: &Derived) { v as &Base; } error[E0605]: non-primitive cast: `&Derived` as `&Base` --> src/main.rs:30:5 | 30 | v as &Base; | ^^^^

Why immutable string can call String::add(mut self, other: &str) [duplicate]

狂风中的少年 提交于 2021-01-28 05:00:30
问题 This question already has an answer here : Why does the compiler not complain that an iterator moved to a for loop is immutable? (1 answer) Closed 15 days ago . In stdlib string.rs: impl Add<&str> for String { type Output = String; #[inline] fn add(mut self, other: &str) -> String { self.push_str(other); self } } let s1 = String::from("tic"); let s2 = String::from("tac"); let s = s1 + &s2;// it works s1 is immutable here, but Add::add( mut self , other: &str) is mut, I just want to know why.

How can I explicitly specify a lifetime when implementing a trait?

别说谁变了你拦得住时间么 提交于 2021-01-28 04:42:39
问题 Given the implementation below, where essentially I have some collection of items that can be looked up via either a i32 id field or a string field. To be able to use either interchangeably, a trait "IntoKey" is used, and a match dispatches to the appropriate lookup map; this all works fine for my definition of get within the MapCollection impl: use std::collections::HashMap; use std::ops::Index; enum Key<'a> { I32Key(&'a i32), StringKey(&'a String), } trait IntoKey<'a> { fn into_key(&'a self

How can I lock the internals of my Rust data structure?

百般思念 提交于 2021-01-28 04:35:38
问题 I'm trying to implement a collection that stores values in both a vector and a hashmap and this is what I have so far: pub struct CollectionWrapper { items: Vec<Item>, items_map: HashMap<ItemKey, Item>, } impl CollectionWrapper { pub fn new() -> Self { CollectionWrapper { items: Vec::new(), items_map: HashMap::new(), } } pub fn add(&mut self, item: Item) { let key = item.get_key(); self.items.push(item.clone()); self.items_map.insert(key, item.clone()); } } I obviously need some kind of lock.

How can I lock the internals of my Rust data structure?

邮差的信 提交于 2021-01-28 04:31:29
问题 I'm trying to implement a collection that stores values in both a vector and a hashmap and this is what I have so far: pub struct CollectionWrapper { items: Vec<Item>, items_map: HashMap<ItemKey, Item>, } impl CollectionWrapper { pub fn new() -> Self { CollectionWrapper { items: Vec::new(), items_map: HashMap::new(), } } pub fn add(&mut self, item: Item) { let key = item.get_key(); self.items.push(item.clone()); self.items_map.insert(key, item.clone()); } } I obviously need some kind of lock.

“cannot recursively call into `Core`” when trying to achieve nested concurrency using Tokio

痞子三分冷 提交于 2021-01-28 04:12:09
问题 I'm building a service that periodically makes an HTTP request. I'm using tokio::timer::Delay as a periodic trigger and hyper to make the HTTP call. Using them together gives me the following error: thread 'tokio-runtime-worker-1' panicked at 'cannot recursively call into `Core`', libcore/option.rs:960:5 How can I make this work? Below is a simplified version of the service. main.rs extern crate futures; extern crate hyper; extern crate tokio; extern crate tokio_core; extern crate tokio_timer

Techniques for turning recursive functions into iterators in Rust?

若如初见. 提交于 2021-01-28 04:00:56
问题 I'm struggling to turn a simple recursive function into a simple iterator. The problem is that the recursive function maintains state in its local variables and call stack -- and to turn this into a rust iterator means basically externalizing all the function state into mutable properties on some custom iterator struct. It's quite a messy endeavor. In a language like javascript or python, yield comes to the rescue. Are there any techniques in Rust to help manage this complexity? Simple

Techniques for turning recursive functions into iterators in Rust?

半城伤御伤魂 提交于 2021-01-28 03:45:37
问题 I'm struggling to turn a simple recursive function into a simple iterator. The problem is that the recursive function maintains state in its local variables and call stack -- and to turn this into a rust iterator means basically externalizing all the function state into mutable properties on some custom iterator struct. It's quite a messy endeavor. In a language like javascript or python, yield comes to the rescue. Are there any techniques in Rust to help manage this complexity? Simple