Câu hỏi phỏng vấn Rust
Câu hỏi

Làm sao để đọc file trong Rust một cách an toàn và xử lý lỗi tốt?

Câu trả lời

markdown Copy
```rust
use std::fs::File;
use std::io::{self, BufRead, BufReader};

fn read_file(path: &str) -> io::Result<()> {
    let file = File::open(path);

    match file {
        Ok(file) => {
            let reader = BufReader::new(file);
            for line in reader.lines() {
                match line {
                    Ok(content) => {
                        // **Xử lý nội dung của dòng**
                        println!("{}", content);
                    }
                    Err(e) => {
                        // **Xử lý lỗi khi đọc dòng**
                        eprintln!("Error reading line: {}", e);
                    }
                }
            }
        }
       ...
middle

middle

Gợi ý câu hỏi phỏng vấn

senior

Bạn làm thế nào để giữ backward compatibility khi phát hành crate mới trong Rust?

junior

Có cách nào để chuyển đổi trực tiếp một Chuỗi thành một số nguyên không?

middle

Trait trong Rust là gì?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào