V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
DTCPSS
V2EX  ›  Rust

Rust 怎么自动猜测子进程 stdout 的字符编码,然后把它转成 String?

  •  
  •   DTCPSS · Jul 1, 2022 · 1956 views
    This topic created in 1397 days ago, the information mentioned may be changed or developed.

    需要读取一个外部程序的字符串输出。捕获到的 stdout 输出是个 Vec<u8>,字符编码在不同系统上可能不一样,比如在 Windows 上是 GB2313 。

    怎么把它转成 Rust 的 String ?

    let process_output: Output = std::process::Command::new("外部程序").output().expect("外部程序执行失败");
    let process_stdout: Vec<u8> = process_output.stdout;
    let process_stdout_string: String = String::from_utf8(process_stdout).expect("编码不是 UTF-8");
    
    2 replies    2022-07-01 14:43:11 +08:00
    DTCPSS
        1
    DTCPSS  
    OP
       Jul 1, 2022
    billzhuang
        2
    billzhuang  
       Jul 1, 2022
    相当于要检测出对应外部程序的 stdout 的编码是么?
    Windows 也不一定就是 GB2312 ,对吧。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1007 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 19:18 · PVG 03:18 · LAX 12:18 · JFK 15:18
    ♥ Do have faith in what you're doing.