Visual Studio Code distorts ANSI characters

I have problems with Swedish national characters in Visual Studio Code. It can be shown with the following program:

fn main() {
let abc = " ååå
 ööö
 äää";
println!("<---{}--->", abc);
}

When I run the program in Rust Playground, I get the following output:

<--- ååå
 ööö
 äää--->

When the program is run from the command line using "cargo run", the output is as follows:

<--- ååå
     ööö
     äää--->

Strangely, spaces are added at the beginning of lines 2 and 3. However, when the program is run in Visual Studio Code, the Swedish characters get distorted.

<--- ååå
     ├╢├╢├╢
     äää--->

Does anybody understand this behavior?
Actually, I work with Regex, and the patterns I'm using don't do what they are supposed to do, and I suspect it to be caused by the character set used in the VSC.

Probably it's the Microsoft ecosystem still trying to force Latin-1 (ISO-8859-1) down your throat? I wouldn't be surprised if, instead of today's sensible default of UTF-8, VS was trying to save your programs in this other encoding.

I think the reason is lack of Unicode in VS Code terminal. Please try that: Enable unicode support in vscode terminal - DEV Community 👩‍💻👨‍💻

Yeah, the file encoding seems to be UTF-8 as expected, but the VSC terminal interprets it as – not even Latin-1 (which doesn’t have those line drawing characters) – but the venerable and nonstandard MS/IBM code page 437 from the early 80s! No doubt due to some particularly misguided backwards compatibility concern.

1 Like

I think there's a way for your program to say that its output is UTF-8 -- Console.OutputEncoding Property (System) | Microsoft Learn mentions SetConsoleOutputCP function - Windows Console | Microsoft Learn, so maybe that?

EDIT: Oh, if it's just in vscode then it's probably not your program's problem.

1 Like

Any ideas on how to solve it? martys71 seems to suggest setting the terminal font to something strange for me: MesloLGS NF. I'm not sure I want to experiment; I don't know how to restore things.

Perhaps you just indented the code by 4 spaces, including inside the string literal?

BTW: These are not "ANSI" characters, they are Unicode characters encoded in UTF-8 (so each Swedish character is two bytes). "ANSI" is an ambiguous term but it typically refers to some single-byte encoding.

1 Like

This is not a VS Code problem. Works fine with VS Code on my Mac.

I've posted an equivalent question on Stack Overflow and it is already solved by editing the VSC settings; see Visual Studio Code distorts ANSI characters for more details.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.