Write cross-platform Rust code by using standard library abstractions like std::env and std::fs to handle OS differences automatically.
Write cross-platform Rust code by using standard library abstractions like std::env::args for arguments and std::fs for file operations, avoiding platform-specific paths or system calls.
use std::env;
use std::fs;
fn main() {
let args: Vec<String> = env::args().collect();
let file_path = &args[2];
let contents = fs::read_to_string(file_path).expect("Failed to read file");
println!("{contents}");
}
Cross-platform Rust code works on Windows, macOS, and Linux without changes because the language handles differences automatically. You use standard tools like std::env to read inputs and std::fs to read files, which adapt to the operating system behind the scenes. Think of it like a universal adapter that lets your code plug into any computer.