코드 리뷰 목록
Rust🤖 AI 리뷰 완료

Rust 안전한 파일 처리

Rust의 소유권 시스템을 활용한 안전한 파일 읽기/쓰기 구현입니다. Result 타입으로 에러를 명시적으로 처리합니다.

J
jung_rust
2025-03-16
2

// 코드

Rust
35 lines
1use std::fs::{self, File};
2use std::io::{self, BufRead, BufReader, Write};
3use std::path::Path;
4
5fn read_lines<P: AsRef<Path>>(path: P) -> io::Result<Vec<String>> {
6    let file = File::open(path)?;
7    let reader = BufReader::new(file);
8    reader.lines().collect()
9}
10
11fn write_file<P: AsRef<Path>>(path: P, content: &str) -> io::Result<()> {
12    let mut file = File::create(path)?;
13    file.write_all(content.as_bytes())?;
14    Ok(())
15}
16
17fn process_file(input: &str, output: &str) -> Result<(), Box<dyn std::error::Error>> {
18    let lines = read_lines(input)?;
19    let processed: Vec<String> = lines
20        .iter()
21        .filter(|line| !line.trim().is_empty())
22        .map(|line| line.trim().to_uppercase())
23        .collect();
24
25    write_file(output, &processed.join("\n"))?;
26    println!("Processed {} lines", processed.len());
27    Ok(())
28}
29
30fn main() {
31    if let Err(e) = process_file("input.txt", "output.txt") {
32        eprintln!("Error: {}", e);
33        std::process::exit(1);
34    }
35}
🤖
AI 코드 리뷰
2025-03-16
자동 분석

`Box<dyn std::error::Error>` 대신 `anyhow::Error`나 커스텀 에러 타입을 사용하는 것을 고려해보세요. 에러 컨텍스트를 더 풍부하게 제공할 수 있습니다.

// 커뮤니티 리뷰1

B
bae_rust
2025-03-16
6

`thiserror` 크레이트로 커스텀 에러를 정의하면 매우 깔끔해집니다. 추천드립니다!

// 리뷰 작성