rust

Write to child process' stdin in Rust?

你离开我真会死。 提交于 2020-12-31 05:50:08
问题 Rust's std::process::Command allows configuring the process' stdin via the stdin method, but it appears that that method only accepts existing files or pipes. Given a slice of bytes, how would you go about writing it to the stdin of a Command ? 回答1: You can create a stdin pipe and write the bytes on it. As Command::output immediately closes the stdin, you'll have to use Command::spawn . Command::spawn inherits stdin by default. You'll have to use Command::stdin to change the behavior. Here is

When should we use unwrap vs expect in Rust

牧云@^-^@ 提交于 2020-12-30 08:33:49
问题 I'm new to rust and trying to understand when should we use unwrap vs expect. Here's a sample code: use std::env; fn main() { let args: Vec<String> = env::args().collect(); let query = args.get(1).unwrap(); println!("query from unwrawp: {}", query); let query = args.get(1).expect("insufficient arguments"); println!("query from expect: {}", query); //$ cargo run hello //OUTPUT: //query from expect: hello //query from unwrawp: hello } Only difference I observed is there's a custom panic message

Can I disable checking for zero division every time the division happens?

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-30 06:17:26
问题 In order to better understand Rusts panic/exception mechanisms, I wrote the following piece of code: #![feature(libc)] extern crate libc; fn main() { let mut x: i32; unsafe { x = libc::getchar(); } let y = x - 65; println!("{}", x); let z = 1 / y; println!("{}", z); } I wanted to check how Rust deals with division by zero cases. Originally I assumed it was either taking an unhandled SIGFPE to the face and dying or it implemented a handler and rerouted it to a panic (which can be dealt with

Capture both stdout & stderr via pipe

可紊 提交于 2020-12-30 02:44:04
问题 I want to read both stderr and stdout from a child process, but it doesn't work. main.rs use std::process::{Command, Stdio}; use std::io::{BufRead, BufReader}; fn main() { let mut child = Command::new("./1.sh") .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn() .unwrap(); let out = BufReader::new(child.stdout.take().unwrap()); let err = BufReader::new(child.stderr.take().unwrap()); out.lines().for_each(|line| println!("out: {}", line.unwrap()) ); err.lines().for_each(|line| println!(

Capture both stdout & stderr via pipe

£可爱£侵袭症+ 提交于 2020-12-30 02:43:29
问题 I want to read both stderr and stdout from a child process, but it doesn't work. main.rs use std::process::{Command, Stdio}; use std::io::{BufRead, BufReader}; fn main() { let mut child = Command::new("./1.sh") .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn() .unwrap(); let out = BufReader::new(child.stdout.take().unwrap()); let err = BufReader::new(child.stderr.take().unwrap()); out.lines().for_each(|line| println!("out: {}", line.unwrap()) ); err.lines().for_each(|line| println!(

Why can I not access a variable declared in a macro unless I pass in the name of the variable?

*爱你&永不变心* 提交于 2020-12-29 09:49:26
问题 I have this macro: macro_rules! set_vars { ( $($x:ident),* ) => { let outer = 42; $( let $x = outer; )* } } Which expands this invocation: set_vars!(x, y, z); into what I expect (from --pretty=expanded ): let outer = 42; let x = outer; let y = outer; let z = outer; In the subsequent code I can print x , y , and z just fine, but outer seems to be undefined: error[E0425]: cannot find value `outer` in this scope --> src/main.rs:11:5 | 11 | outer; | ^^^^^ not found in this scope I can access the

panic with "thread 'main' panicked at 'not currently running on the Tokio runtime

≡放荡痞女 提交于 2020-12-29 09:22:47
问题 I'm using the example code on elastic search's blog post about their new crate and I'm unable to get it working as intended. The thread panics with thread 'main' panicked at 'not currently running on the Tokio runtime.' . What is the Tokio runtime, how do I configure it and why must I? use futures::executor::block_on; async elastic_search_example() -> Result<(), Box<dyn Error>> { let index_response = client .index(IndexParts::IndexId("tweets", "1")) .body(json!({ "user": "kimchy", "post_date"

panic with "thread 'main' panicked at 'not currently running on the Tokio runtime

旧巷老猫 提交于 2020-12-29 09:21:41
问题 I'm using the example code on elastic search's blog post about their new crate and I'm unable to get it working as intended. The thread panics with thread 'main' panicked at 'not currently running on the Tokio runtime.' . What is the Tokio runtime, how do I configure it and why must I? use futures::executor::block_on; async elastic_search_example() -> Result<(), Box<dyn Error>> { let index_response = client .index(IndexParts::IndexId("tweets", "1")) .body(json!({ "user": "kimchy", "post_date"

What is a clean way to convert a Result into an Option?

我的未来我决定 提交于 2020-12-29 08:49:16
问题 Before updating to a more recent Rust version the following used to work: fn example(val: &[&str]) { let parsed_value: Vec<usize> = val .iter() .filter_map(|e| e.parse::<usize>()) .collect(); } However, now the parse method returns a Result type instead of an Option and I get the error: error[E0308]: mismatched types --> src/lib.rs:4:25 | 4 | .filter_map(|e| e.parse::<usize>()) | ^^^^^^^^^^^^^^^^^^ expected enum `std::option::Option`, found enum `std::result::Result` | = note: expected type

Is there an intrinsic reason explaining why Rust does not have higher-kinded-types?

岁酱吖の 提交于 2020-12-29 05:54:43
问题 Rust does not have higher-kinded-types. For example, functor (and thus monad) cannot be written in Rust. I would like to know if there is a deep reason explaining this and why. For instance, reason that I can understand can be that there is no zero-cost abstraction making HKT possible. Or type inference is significantly more difficult. And of course, I also looking for an explaination showing me why this is a real limitation. If the anwer was already given somewhere else, could you give me