Rust Sample - format!

mohadang·2023년 7월 30일
0

Rust Sample

목록 보기
15/16
post-thumbnail
use std::io::{self};

fn main() {
    let mut input = String::new();
    if let Ok(_) = io::stdin().read_line(&mut input) {
        if let Ok(n) = input.trim().parse::<usize>() {
            let max_stars_width = 1 + ((n - 1) * 2);

            let mut v: Vec<String> = Vec::with_capacity(n * 2);
            let mut stars_width = 1;
            (0 .. n - 1).for_each(|_| {
                let stars: String = (0..stars_width).map(|_| '*').collect();
                let stars = format!("{:^width$}", stars, width = max_stars_width);
                let stars = stars.trim_end().to_string();
                v.push(stars);
                stars_width += 2;
            });

            for item in v.iter() {
                println!("{item}");
            }

            let stars: String = (0..max_stars_width).map(|_| '*').collect();
            println!("{stars}");
            
            for item in v.iter().rev() {
                println!("{item}");
            }
        }
    }
}
profile
mohadang

0개의 댓글