在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())
        }
    }
}
相关推荐
qiuhaipeng124 分钟前
Claude code 升级后上下文长度变短问题
java·前端·数据库
zzz_236829 分钟前
个人 AI 记忆如何跨工具复用:用 Markdown、索引和 Skill 搭一个可治理的记忆库
前端·人工智能·react.js·前端框架·agent·agent测评
剑之所向2 小时前
.NET 官方`System.Threading.Channels`
前端·javascript·数据库
计算机魔术师3 小时前
GPT-6 Astra幻觉砍到2%,却被一种老招数轻松绕过
前端
cjy0001113 小时前
2026AI 产品如何从一次性 Demo 变成可重复使用的业务工具?
前端·人工智能·fde
IT_陈寒3 小时前
React的状态更新坑得我差点加班到天亮
前端·人工智能·后端
风骏时光牛马3 小时前
深挖底层逻辑:XX源码深度拆解分析
前端
渡我白衣4 小时前
并查集:基础认识与模拟实现
android·java·javascript·数据结构·c++·算法·并查集
电商API_180079052474 小时前
电商商品价格监控工具淘宝京东商品价格抓取API项目实操分享
java·大数据·开发语言·前端·数据挖掘
sxmas4 小时前
用状态机与DAG建模四阶段业务流程:以MIND模型为例
前端