#[derive(Debug)] pub struct PascalsTriangle { rows: Vec>, } impl PascalsTriangle { fn next_row(row: &[u32]) -> Vec { let mut next = Vec::new(); let last = row.iter().fold(0, |p, n| { next.push(p + n); *n }); next.push(last); next } } impl PascalsTriangle { pub fn new(row_count: u32) -> Self { let mut rows = Vec::new(); if row_count > 0 { rows.push(vec![1]); (1..row_count).for_each(|i| { let irow = Self::next_row(&rows[(i as usize) - 1]); rows.push(irow); }) } PascalsTriangle { rows } } pub fn rows(&self) -> Vec> { self.rows.clone() } }