You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解决Windows下Visual Studio Code中瑞典字符显示乱码问题

问题描述

在Windows 11系统的Visual Studio Code中开发Rust时,瑞典特殊字符出现显示异常:

  • 命令行执行cargo run,字符显示正常但存在多余空格;
  • VS Code内置终端运行时,字符直接乱码。

本人从事文本处理工作,该问题影响较大。

环境信息:

  • Windows 11 Pro 10.0.22621.22621
  • Visual Studio Code 1.73.1(用户安装版),基于Chromium 102.0.5005.167、Node.js 16.14.2,未启用沙箱
解决方案

1. 强制VS Code终端使用UTF-8编码

打开VS Code的settings.json(快捷键Ctrl+,打开设置面板,点击右上角的打开设置图标),添加或修改以下配置:

"terminal.integrated.profiles.windows": {
    "PowerShell": {
        "source": "PowerShell",
        "args": ["-NoExit", "/c", "chcp 65001"]
    },
    "Command Prompt": {
        "path": ["${env:windir}\\System32\\cmd.exe"],
        "args": ["/k", "chcp 65001"]
    }
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.shellArgs.windows": ["-NoExit", "/c", "chcp 65001"]

保存配置后重启VS Code,再运行cargo run测试。

2. 在Rust代码中指定UTF-8输出

确保代码输出的字节流为UTF-8格式,避免系统默认编码干扰。可以在代码中添加编码设置:

use std::io::{self, Write};

fn main() {
    // 强制标准输出使用UTF-8编码
    if let Err(e) = io::stdout().set_write_encoding(io::WriteEncoding::Utf8) {
        eprintln!("Failed to set UTF-8 encoding: {}", e);
    }
    // 测试瑞典字符输出
    println!("测试瑞典字符: åäöÅÄÖ");
}

如果需要处理更多编码场景,可引入encoding_rs crate来做字符编码转换。

3. 确认Rust源文件编码为UTF-8

  • 打开Rust文件后,点击VS Code右下角的编码标识(如GB2312),选择Reopen with encoding,再选UTF-8;
  • 在settings.json中添加默认编码配置:
"files.encoding": "utf8",
"files.defaultLanguage": "rust"

4. 更新VS Code到最新版本

当前使用的VS Code 1.73.1版本较旧,更新到最新版本可能修复内置终端的编码bug:
打开VS Code,点击左下角设置图标,选择Check for Updates,安装更新后重启软件。

内容的提问来源于stack exchange,提问作者Kalle Svensson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 16:45:34