Unable to see output from Rust program in Visual Studio Code


#1

Hi, I am new to Rust and am trying to set up Visual Studio Code in Windows.

I want the configuration to be similar to Visual studio:

  • Select Debug/Release and run the corresponding cargo command
  • Press F5 to run program in Debugger, Ctrl+F5 to run program without Debugger.

To do so, I installed the latest stable-msvc toolchain (1.32.0), alongwith rls and rustfmt, and the Codelldb+rls extensions for Visual Studio Code.

However, I cannot seem to make the program I wrote output anything on any console in VS Code. Debugging works like a charm though with lldb.

These are my config files:

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug",
            "sourceLanguages": [
                "rust"
            ],
            "preLaunchTask": "cargo build",
            "internalConsoleOptions": "openOnSessionStart",
            "program": "${workspaceRoot}/target/debug/${workspaceRootFolderName}",
            "args": []
        },
        
        {
            "type": "lldb",
            "request": "launch",
            "name": "Release",
            "sourceLanguages": [
                "rust"
            ],
            "preLaunchTask": "cargo build release",
            "internalConsoleOptions": "openOnSessionStart",
            "program": "${workspaceRoot}/target/release/${workspaceRootFolderName}",
            "args": []
        }
    ]
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cargo build",
            "type": "shell",
            "command": "cargo",
            "args": [
                "build"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": "$rustc"
        },

        {
            "label": "cargo build release",
            "type": "shell",
            "command": "cargo",
            "args": [
                "build",
                "--release"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": "$rustc"
        }
    ]
}

This is also my first time using VS Code so I am most likely doing something stupid. I would be really grateful if someone pointed me towards what I’m doing wrong, and if possible, setting VS Code like I mentioned at the beginning of the post, if it is something that can be done. Thanks in advance!


#2

Two thoughts, maybe irrelevant since I’m not on Windows.
Check you have the output selected in the view. (VSC does not just stick eveything in one.)
Is build code with #![windows_subsystem = "console"]


#3

Thank you for the fast reply!

Well, it seems adding the #![windows_subsystem = "console"] to main.rs did not do anything noticeable. However, as you said, it seems to be outputting to lldb.
Now I can see the output in the lldb window, thanks!

However, I am a bit curious. Is it normal to show the program output inside the lldb window? Is that how it’s done usually, or is there another way, for example, not running the program through lldb for release, and through lldb for debug?


#4

in VScode, all sorts of things run under the hood, including debugger, git, and other extensions, log things to an Output window, and you need to select the right one, as you seem to have found.

When not running in the debugger, I tend to just use “cargo run --release” in the shell window, so output goes there. There’s also an option to “run without debug” which I assume is more similar to the lldb pattern and you could configure to use a release build


#5

Indeed. It turns out there’s a separate shortcut, “Run Program” (Ctrl+Alt+N) to do just that. By installing the CodeRunner extension and adding the executorMap in settings.json to:

 "code-runner.runInTerminal": true,
 "code-runner.executorMap": {
        "rust": "cargo run --release"
    },

I am now able to run release builds using the keyboard shortcut Ctrl+Alt+N.

I guess that clears up all my questions for now.

Thanks a lot for the help everyone!