rust

How do I pass an enum variant to match on as a function parameter?

浪子不回头ぞ 提交于 2020-01-14 03:36:14
问题 I would like to pass in the parameters what arm of the enum I need to match, something like this: enum D { A(i64), B(u64), C(u64, u64), } let a = D.A(10); println!(a.is_of(D.A)); // true println!(a.is_of(D.B)); // false I know I can use matching rules for this, but I'd like this is_of method to take as an input of the enum options for my purposes. 回答1: You cannot. It is not possible to pass types as function parameters. Enum variants are not types to start with. If you are OK using a macro

Redirect output of child process spawned from Rust

心已入冬 提交于 2020-01-13 18:08:57
问题 I need to redirect output of a spawned child process. This is what I tried but it doesn't work: Command::new(cmd) .args(&["--testnet", "--fast", format!("&>> {}", log_path).as_str()]) .stdin(Stdio::piped()) .stdout(Stdio::inherit()) .spawn() 回答1: To directly use a file as output without intermediate copying from a pipe, you have to pass the file descriptor. The code is platform-specific, but with conditional compilation you can make it work on Windows too. let f = File::create("foo.log")

Is it documented that Cargo can download and bundle multiple versions of the same crate?

落花浮王杯 提交于 2020-01-13 16:22:10
问题 By forking and playing with some code, I noticed that Cargo can download and bundle multiple versions of the same crate in the same project (native-tls 0.1.5 and 0.2.1, for example). I have wasted so much time by looking at the documentation of the wrong version. I have looked for some information about this behaviour but I was not able to find anything. Is this documented somewhere? Is there an easy way to determine/detect the version used by the code you're working on (current edited file)?

How do I make many different structs that all implement the same trait comparable to each other?

元气小坏坏 提交于 2020-01-13 14:56:38
问题 In Java-lingo, I have an interface R , an interface RT extends R (where RT implements all of R ) and a bunch of other classes that all implement RT . Transitioning to Rust I ended up with two traits trait R { ... } trait RT { ... } where RT is a "subtrait" of R : impl R for X where X: RT { ... } Following that I have a bunch of structs, all of which implement RT : struct RV { ... } impl RT for RV { ... } struct I { ... } impl RT for I { ... } struct U { ... } impl RT for U { ... } // ... So

Borrowing reference in structure

杀马特。学长 韩版系。学妹 提交于 2020-01-13 13:05:36
问题 I'm trying to put in one structure information about inotify events and hashmap with inotify watch id as key and name of the file as value. extern crate inotify; use inotify::INotify; use std::sync::{Arc, Mutex}; use std::collections::HashMap; struct Notificator { inotify: INotify, watch_service: Arc<Mutex<HashMap<inotify::wrapper::Watch, Arc<String>>>>, } impl Notificator { pub fn new() -> Notificator { Notificator { inotify: INotify::init().unwrap(), watch_service: Arc::new(Mutex::new

Iterating over a range of generic type

蹲街弑〆低调 提交于 2020-01-13 11:08:08
问题 I have a trait trait B { type Index: Sized + Copy; fn bounds(&self) -> (Self::Index, Self::Index); } I want to get all the Index es within bounds : fn iterate<T: B>(it: &T) { let (low, high) = it.bounds(); for i in low..high {} } This won't work since there's no constraint that the type T can be "ranged" over, and the compiler says as much: error[E0277]: the trait bound `<T as B>::Index: std::iter::Step` is not satisfied --> src/main.rs:8:5 | 8 | for i in low..high {} | ^^^^^^^^^^^^^^^^^^^^^

Iterating over a range of generic type

血红的双手。 提交于 2020-01-13 11:06:41
问题 I have a trait trait B { type Index: Sized + Copy; fn bounds(&self) -> (Self::Index, Self::Index); } I want to get all the Index es within bounds : fn iterate<T: B>(it: &T) { let (low, high) = it.bounds(); for i in low..high {} } This won't work since there's no constraint that the type T can be "ranged" over, and the compiler says as much: error[E0277]: the trait bound `<T as B>::Index: std::iter::Step` is not satisfied --> src/main.rs:8:5 | 8 | for i in low..high {} | ^^^^^^^^^^^^^^^^^^^^^

Is this the right way to read lines from file and split them into words in Rust?

旧街凉风 提交于 2020-01-13 11:05:24
问题 Editor's note: This code example is from a version of Rust prior to 1.0 and is not syntactically valid Rust 1.0 code. Updated versions of this code produce different errors, but the answers still contain valuable information. I've implemented the following method to return me the words from a file in a 2 dimensional data structure: fn read_terms() -> Vec<Vec<String>> { let path = Path::new("terms.txt"); let mut file = BufferedReader::new(File::open(&path)); return file.lines().map(|x| x

Why is Fn derived from FnMut (which is derived from FnOnce)?

好久不见. 提交于 2020-01-13 10:34:21
问题 If you look in the official Rust doc, you see that the trait Fn is derived from FnMut , or, to implement Fn , you have to implement FnMut (and after that FnOnce since FnMut also derives from it). Why is that so? I simply can't comprehend that. Is it because you can call every Fn as a FnOnce or FnMut ? 回答1: The best reference for this is the excellent Finding Closure in Rust blog post. I'll quote the salient part: There’s three traits, and so seven non-empty sets of traits that could possibly

Casting away lifetime constraints?

烂漫一生 提交于 2020-01-13 10:16:12
问题 I'm trying to write a Rust function that casts an input from one lifetime constraint to a same-typed output with a global lifetime constraint (conceptually something like unsafe fn foo<'a, T1, T2>(x: T1) -> T2 where T1: 'a, T2 = T1 + 'static ), but I can't quite figure out how to write it without adding indirection layers like Box . Any ideas? More generally, I'm trying to implement an unsafe thread::scoped in terms of mem::transmute and thread::spawn . spawn requires 'static bounds on its T