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!
π¦ 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.
Install VS Code
Download from code.visualstudio.com
βοΈ Step 1: Create the Perfect Split View
Create Test Files First
- Create
Hello.rs(code below) - Create
input.txt - Create
output.txt(will be generated)
Split View Setup
To create the perfect 3-panel view:
-
First split - Open
Hello.rs- Go to View β Editor Layout β Split Left
- This separates Rust file to the left
-
Second split - Click on
input.txt- View β Editor Layout β Split Right
- Now you have Rust | input.txt side by side
-
Perfect 3-panel view - Click on
output.txt- View β Editor Layout β Split Right
- Final layout:
Hello.rs|input.txt|output.txt
π§ͺ Step 2: The Magic tasks.json
Configure Tasks
- Open Terminal β Configure Tasks β Create tasks.json file from template β Others
- 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
}
}
]
}
πΉ For Windows (CMD Only)
Note: β οΈ This will not work if your default terminal is PowerShell.
Change your default terminal to Command Prompt (cmd):
PressCtrl+Shift+Pβ typedefault 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
}
}
]
}
β 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
-
Windows:
π§ͺ 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!");
}
input.txt
5
10
3
7
1
βΆοΈ Step 4: RUN & Verify! (30 seconds)
Windows: Ctrl+Shift+B | macOS: Cmd+Shift+B
Watch in real-time:
- Terminal shows compilation
-
output.txtgets populated automatically - 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!
π― Perfect for Learning & Prototyping
Your setup is now battle-tested:
- β
Lightning compilation (
rustcis 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)