visual-studio-coderust

How to configure run/debug bar tasks?


enter image description here

How do I configure what's executed when pressing Run or Debug here? This is what I see:

> Executing task: cargo run --package myproject --bin myproject <

I tried modifying tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cargo",
            "command": "run",
            "problemMatcher": [
                "$rustc"
            ],
            "label": "rust: cargo run",
            "env": {
                "RUST_LOG": "info"
            }
        },
        {
            "type": "cargo",
            "command": "build",
            "problemMatcher": [
                "$rustc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "label": "rust: cargo build",
            "env": {
                "RUST_LOG": "info"
            }
        }
    ]
}

but it has no effect on the task.

I'm trying to set an environment variable ("RUST_LOG": "info").


Solution

  • Using rust-analyzer then Setting runnable environment variables:
    You may add this to your vscode settings.json e.g. ~/.config/Code/User/settings.json

    "rust-analyzer.runnables.extraEnv": {
        "RUST_LOG": "info"
    },
    

    Or press ctrl+comma then click on top right corner on Open Settings (JSON) icon.

    And then you are good to Rust:

    use std::env;
    
    fn main() -> std::io::Result<()> {
        let key = "RUST_LOG";
        match env::var(key) {
            Ok(val) => println!("{}: {:?}", key, val),
            Err(e) => println!("couldn't interpret {}: {}", key, e),
        }
    
        Ok(())
    }
    

    Output:

    RUST_LOG: "info"