Skip to content
← All examples

Sum of squares

Rust Lambdas

closure |x| x * x

Flowchart (ISO 5807)

Starttotal = 0i = 1, 5, 1total += square(i)Output «Сума квадратів: » totalEndFigure 1 — mainStartInput xReturn x * xEndFigure 2 — square

Source code

fn main() {
    let square = |x: i32| x * x;
    let mut total = 0;
    for i in 1..=5 {
        total += square(i);
    }
    println!("Сума квадратів: {}", total);
}