在rust中执行命令行输出中文乱码解决办法

如果你使用标准的依赖库执行命令中包含中文的话, 就会发现中文乱码,如果你的输出中没有中文,就可以正常输出,因为windows的命令行默认使用的是gbk编码。。。。。

rust 复制代码
#[tauri::command]
pub async fn run_command(command: String) -> Result<String, String> {
    #[cfg(target_os = "windows")]
    let output = {
        // 先设置控制台编码为UTF-8
        let _ = tokio::process::Command::new("powershell")
            .arg("-Command")
            .arg("chcp 65001 | Out-Null")
            .creation_flags(0x08000000)
            .status()
            .await;
        tokio::process::Command::new("powershell")
            .arg("-Command")
            .arg(&command)
            .creation_flags(0x08000000)
            .output()
            .await
            .map_err(|e| e.to_string())?
    };

    #[cfg(not(target_os = "windows"))]
    let output = tokio::process::Command::new("sh")
        .arg("-c")
        .arg(&command)
        .output()
        .await
        .map_err(|e| e.to_string())?;

    if output.status.success() {
        print!(
            "Command output: {}",
            String::from_utf8_lossy(&output.stdout)
        );
        Ok(String::from_utf8_lossy(&output.stdout).to_string())
    } else {
        Err(String::from_utf8_lossy(&output.stderr).to_string())
    }
}

解决办法

使用标准的编码依赖库encoding_rs = "0.8"

完整代码:

rust 复制代码
#[tauri::command]
pub async fn run_command(command: String) -> Result<String, String> {
    #[cfg(target_os = "windows")]
    let output = tokio::process::Command::new("powershell")
        .arg("-Command")
        .arg(&command)
        .creation_flags(0x08000000)
        .output()
        .await
        .map_err(|e| e.to_string())?;

    #[cfg(not(target_os = "windows"))]
    let output = tokio::process::Command::new("sh")
        .arg("-c")
        .arg(&command)
        .output()
        .await
        .map_err(|e| e.to_string())?;

    if output.status.success() {
        #[cfg(target_os = "windows")]
        {
            // 在Windows上尝试从GBK转换为UTF-8
            let (decoded, _, _) = GBK.decode(&output.stdout);
            Ok(decoded.into_owned())
        }
        #[cfg(not(target_os = "windows"))]
        {
            Ok(String::from_utf8_lossy(&output.stdout).to_string())
        }
    } else {
        #[cfg(target_os = "windows")]
        {
            let (decoded, _, _) = GBK.decode(&output.stderr);
            Err(decoded.into_owned())
        }
        #[cfg(not(target_os = "windows"))]
        {
            Err(String::from_utf8_lossy(&output.stderr).to_string())
        }
    }
}
相关推荐
EchoEcho8 小时前
记录overflow:hidden和scrollIntoView导致的页面问题
前端·css
Cache技术分享8 小时前
318. Java Stream API - 深入理解 Java Stream 的中间 Collector —— mapping、filtering 和 fla
前端·后端
竟未曾年少轻狂8 小时前
Vue3 生命周期钩子
前端·javascript·vue.js·前端框架·生命周期
TT哇8 小时前
【实习】数字营销系统 银行经理端(interact_bank)前端 Vue 移动端页面的 UI 重构与优化
java·前端·vue.js·ui
蓝帆傲亦8 小时前
Web前端跨浏览器兼容性完全指南:构建无缝用户体验的最佳实践
前端
晴殇i8 小时前
【前端缓存】localStorage 是同步还是异步的?为什么?
前端·面试
不一样的少年_8 小时前
Chrome 插件实战:如何实现“杀不死”的可靠数据上报?
前端·javascript·监控
深度涌现8 小时前
DNS详解——域名是如何解析的
前端
小码哥_常8 小时前
Android内存泄漏:成因剖析与高效排查实战指南
前端
卤代烃8 小时前
✨ 形势比人强,Chrome 大佬也去搞 Gemini 了
前端·agent·vibecoding