■ 디렉토리 트리 구조를 출력하는 방법을 보여준다.
▶ 예제 코드 (RS)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
use std::borrow; use std::env; use std::path; fn main() { let argument_vector : Vec<String> = env::args().collect(); let mut source_directory_path : &str = "."; if argument_vector.len() >= 2 { source_directory_path = &argument_vector[1]; } let source_path_buffer : path::PathBuf = path::PathBuf::from(source_directory_path); println!("{}", source_directory_path); tree(&source_path_buffer, 0); } fn tree(parent_path_buffer : &path::PathBuf, level : isize) { let child_read_directory = parent_path_buffer.read_dir().expect("존재하지 않는 경로입니다"); for child_directory_entry_result in child_read_directory { let child_path_buffer = child_directory_entry_result.unwrap().path(); for _ in 1..=level { print!("| "); } let file_name : borrow::Cow<str> = child_path_buffer.file_name().unwrap().to_string_lossy(); if child_path_buffer.is_dir() { println!("|-- <{}>", file_name); tree(&child_path_buffer, level + 1); continue; } println!("|-- {}", file_name); } } |