IO Functions
Functions สำหรับ Input/Output operations
Reading from Stdin
Section titled “Reading from Stdin”use std::io::{self, BufRead, Write};
fn main() { // Read line print!("Enter your name: "); io::stdout().flush().unwrap();
let mut name = String::new(); io::stdin().read_line(&mut name).expect("Failed to read"); let name = name.trim();
println!("Hello, {}!", name);
// Read multiple lines println!("Enter numbers (empty line to stop):"); let stdin = io::stdin(); let mut numbers: Vec<i32> = Vec::new();
for line in stdin.lock().lines() { let line = line.expect("Failed to read"); if line.is_empty() { break; } if let Ok(n) = line.parse() { numbers.push(n); } }
println!("Sum: {}", numbers.iter().sum::<i32>());}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google