DEV Community

DobbyTheDev
DobbyTheDev

Posted on

πŸš€ Ultimate VS Code Setup for Rust - Perfect for Learning & Fast Prototyping

Lightning-fast compilation with automatic input/output redirection for all major OS!

Hey devs! πŸ‘‹ Starting with Rust? Want instant compilation and automatic I/O handling right in VS Code? This guide sets up a production-ready environment perfect for learning, prototyping, and rapid development.

No more terminal switching - just one shortcut and you're done!

Screenshot: VS Code with Rust file open, terminal showing successful compilation and output


πŸ“¦ Prerequisites

Install Rust

Follow the official Rust installation guide for your operating system:

  • Windows: Download rustup-init.exe
  • macOS: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • Linux: Same curl command as macOS

After installation:

Verify: rustc --version works in BOTH external terminal AND VS Code terminal

πŸ’‘ Pro Tip: If rustc --version works externally but NOT in VS Code terminal, VS Code cached old PATH. Force-quit All VS Code processes + kill terminal instances and restart fresh.

Screenshot: Terminal showing rustc --version output in VS Code terminal

Install VS Code

Download from code.visualstudio.com


βš™οΈ Step 1: Create the Perfect Split View

Create Test Files First

  1. Create Hello.rs (code below)
  2. Create input.txt
  3. Create output.txt (will be generated)

Split View Setup

To create the perfect 3-panel view:

  1. First split - Open Hello.rs
    • Go to View β†’ Editor Layout β†’ Split Left
    • This separates Rust file to the left

Screenshot: First split - Rust file on left

  1. Second split - Click on input.txt
    • View β†’ Editor Layout β†’ Split Right
    • Now you have Rust | input.txt side by side

Screenshot: Second split - Rust file | input.txt

  1. Perfect 3-panel view - Click on output.txt
    • View β†’ Editor Layout β†’ Split Right
    • Final layout: Hello.rs | input.txt | output.txt

Screenshot: All Tabs Open


πŸ§ͺ Step 2: The Magic tasks.json

Configure Tasks

  1. Open Terminal β†’ Configure Tasks β†’ Create tasks.json file from template β†’ Others
  2. Clear the default content and replace it with the snippet for your operating system below:

πŸ”Ή For macOS / Linux

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Rust compile and run",
            "type": "shell",
            "command": "rustc -o ${fileBasenameNoExtension} ${file} && ./${fileBasenameNoExtension} < input.txt > output.txt",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή For Windows (CMD Only)

Note: ⚠️ This will not work if your default terminal is PowerShell.

Change your default terminal to Command Prompt (cmd):

Press Ctrl+Shift+P β†’ type default terminal β†’ choose Command Prompt

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Rust compile and run (Windows CMD)",
      "type": "shell",
      "command": "",
      "args": [
        "copy",
        "\\"${file}\\"",
        "temp.rs",
        "&&",
        "rustc",
        "temp.rs",
        "-o",
        "${fileBasenameNoExtension}",
        "&&",
        "${fileBasenameNoExtension}",
        "<",
        "input.txt",
        ">",
        "output.txt",
        "&&",
        "del",
        "temp.rs",
        "&&",
        "del",
        "${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "shell": {
          "executable": "cmd.exe",
          "args": ["/c"]
        }
      },
      "presentation": {
        "reveal": "never"
      },
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

βœ… What This Does

  • πŸ¦€ Compiles Hello.rs β†’ Hello / Hello.exe
  • πŸ“₯ Reads automatically from input.txt
  • πŸ“€ Writes output to output.txt
  • ⚑ Runs with one shortcut:
    • Windows: Ctrl+Shift+B
    • macOS/Linux: Cmd+Shift+B

πŸ§ͺ Step 3: Test Input/Output Files

Just paste this code in your rust file - don't worry about what it does. This is purely to test if input/output redirection works correctly:

Hello.rs

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

fn main() {
    let stdin = io::stdin();
    let mut numbers: Vec<i32> = Vec::new();

    println!("=== Rust I/O Test ===");

    // Read ALL lines from stdin
    for (i, line) in stdin.lock().lines().enumerate() {
        match line {
            Ok(input) => {
                if input.trim().is_empty() {
                    break;
                }
                if let Ok(num) = input.trim().parse::<i32>() {
                    numbers.push(num);
                    println!("Read line {}: {}", i+1, num);
                } else {
                    println!("Read string '{}'", input.trim());
                }
            }
            Err(_) => break,
        }
    }

    println!("\n--- Results ---");
    println!("Numbers read: {}", numbers.len());

    if !numbers.is_empty() {
        let sum: i32 = numbers.iter().sum();
        println!("Sum: {}", sum);
        println!("Average: {:.2}", sum as f64 / numbers.len() as f64);
        println!("Max: {}", numbers.iter().max().unwrap());
        println!("Min: {}", numbers.iter().min().unwrap());
    }

    println!("Test complete!");
}
Enter fullscreen mode Exit fullscreen mode

input.txt

5
10
3
7
1

Enter fullscreen mode Exit fullscreen mode

Screenshot: VS Code 3-panel view with all test files open


▢️ Step 4: RUN & Verify! (30 seconds)

Windows: Ctrl+Shift+B | macOS: Cmd+Shift+B

Watch in real-time:

  1. Terminal shows compilation
  2. output.txt gets populated automatically
  3. Perfect I/O redirection!

Expected output.txt:

=== Rust I/O Test ===
Read line 1: 5
Read line 2: 10
Read line 3: 3
Read line 4: 7
Read line 5: 1

--- Results ---
Numbers read: 5
Sum: 26
Average: 5.20
Max: 10
Min: 1
Test complete!
Enter fullscreen mode Exit fullscreen mode

Screenshot: VS Code with Rust file open, terminal showing successful compilation and output


🎯 Perfect for Learning & Prototyping

Your setup is now battle-tested:

  • βœ… Lightning compilation (rustc is super fast)
  • βœ… Automatic I/O redirection (no manual piping)
  • βœ… Perfect split view (code | input | output)
  • βœ… Cross-platform (Windows/macOS/Linux)
  • βœ… One shortcut (Ctrl/Cmd+Shift+B)

This approach also works great for competitive programming on Codeforces, LeetCode, AtCoder, etc!


Questions? Drop 'em in comments! πŸ‘‡

Happy Rusting! πŸ¦€


Reference: How to set up Visual Studio Code for C++ (takeuforward.org)

Top comments (0)