微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

夹心管生锈了怎么办?

如何解决夹心管生锈了怎么办?

我想创建一个 rust 程序,它通过管道从外部程序输入并通过管道吐到另一个外部程序,就像三明治一样。更具体地说,我正在尝试创建的玩具模型是“gzip -cd input | rust | gzip -c -> output”。

基本上,我知道如何做第一部分(管道输入),例如:


save_groupwise <- function(data,group_col,path = ""){
  
  # Split into subsets
  splits <- split(x = data,f = data[[group_col]])
  
  # Save each subset
  purrr::map2(
      .x = splits,.y = names(splits),.f = function(.x,.y){
        readr::write_csv(.x,path = paste0(path,.y,".csv"))
      }
    )
  
  # Don't return anything
  invisible()
}

iris %>% 
  save_groupwise("Species")
  

但我被困在第二部分,也不知道如何将两者拼凑在一起。我知道 Perl 可以优雅地做到这一点:

   let child = match Command::new("zcat")
        .args(&["input"])
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn();
   let filehand = BufReader::new(child.stdout.unwrap());
   for line in filehand.lines() {
       ...
   }

优雅我的意思是,in 和 out 的外部处理对主程序透明:您只需要将第一个管道视为标准输入,将第二个管道视为标准输出,无需再担心.我想知道是否有人知道如何在 Rust 中实现类似的功能,谢谢。

解决方法

你可以这样做:

use std::io::{BufRead,BufReader,Write};
use std::process::{Command,Stdio};

pub fn main() {
    let mut left_child = Command::new("/bin/ls")
        .stdout(Stdio::piped())
        .spawn()
        .expect("failed to execute child");

    let mut right_child = Command::new("/bin/cat")
        .stdin(Stdio::piped())
        .spawn()
        .expect("failed to execute child");

    // extra scope to ensure that left_in and right_out are closed after
    // copying all available data
    {
        let left_in = BufReader::new(left_child.stdout.take().unwrap());
        let mut right_out = right_child.stdin.take().unwrap();
        for line in left_in.lines() {
            writeln!(&mut right_out,"{}",line.unwrap()).unwrap();
        }
    }
    let left_ecode = left_child.wait().expect("failed to wait on child");
    let right_ecode = right_child.wait().expect("failed to wait on child");

    assert!(left_ecode.success());
    assert!(right_ecode.success());
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。