Pascal Triangle Row With In-Place Pass

Grow each row from all ones by folding right-to-left inside every level, so a single vector yields any Pascal row without recursion.

RustData Integrity

Code

rustsnippet
16 lines
fn pascal_row(row: usize) -> Vec<u64> {
    // Build the row in place: each level folds right-to-left
    let mut line = vec![1u64; row + 1];
    for i in 2..=row {
        for j in (1..i).rev() {
            line[j] += line[j - 1];
        }
    }
    line
}

fn main() {
    for r in [0, 1, 4, 6] {
        println!("row {r}: {:?}", pascal_row(r));
    }
}

Inside the snippet

Pascal Triangle Row With In-Place Pass reduces to the functions pascal_row and main.

Repetition is confined to for i in 2..=row — the rest of the file is straight-line.

Using it

The entry-point signature is fn pascal_row(row: usize) -> Vec<u64>

The run prints row — each prefix names the step that produced it.

main sequences pascal_row; executing the file runs that flow once.

Key statements

fn pascal_row(row: usize) -> Vec<u64> {
    // Build the row in place: each level folds right-to-left
    let mut line = vec![1u64; row + 1];
    for i in 2..=row {
        for j in (1..i).rev() {
            line[j] += line[j - 1];
        }
    }
    line
}

Interactive Tools for This Topic

FAQ

What does Pascal Triangle Row With In-Place Pass print when run?
Run it as a complete program — the output lines carry labels such as `row`.