FS Functions
File system functions ใน Rust
Basic File Operations
Section titled “Basic File Operations”use std::fs;
fn main() -> std::io::Result<()> { // ============================================ // Read entire file // ============================================
let contents = fs::read_to_string("/etc/hostname")?; println!("Hostname: {}", contents.trim());
// ============================================ // Read as bytes // ============================================
let bytes = fs::read("/etc/hostname")?; println!("Bytes: {:?}", &bytes[..5.min(bytes.len())]);
// ============================================ // Write string // ============================================
fs::write("/tmp/test.txt", "Hello, World!")?;
// ============================================ // Write bytes // ============================================
fs::write("/tmp/binary.dat", &[0u8, 1, 2, 3, 4])?;
// Verify println!("Written: {}", fs::read_to_string("/tmp/test.txt")?);
Ok(())}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google