Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 15, 2022 06:27 pm GMT

Closures in rust / Closures en rust

Image description
Github repository

cargo new closures
cd closures

The utility of a closure is basically to declare a variable that can only be modified externally within the same function. In this case x waits for the values that will be assigned to it in the main function, and then proceeds with the code available in "closures_example".

fn closures_example(first_number: i32, second_number: i32) -> i32 {    let x_number = |x| {        println!("Multiply {} by 10", x);        x * 10    };    if first_number > second_number {        x_number(first_number)    } else {        x_number(second_number)    }}fn main() {    let a = closures_example(9, 125);    println!("{}", a);    let b = closures_example(23, 72);    println!("{}", b);}
cargo run

Image description

cargo new closures
cd closures

La utilidad de un los closures son bsicamente declarara una variable que solo puede ser modificada externamente dentro de una misma funcin. En este caso x espera los valeres que le sern asignados en la funcin main , para luego proceder con el cdigo disponible en "closures_example".

fn closures_example(first_number: i32, second_number: i32) -> i32 {    let x_number = |x| {        println!("Multiply {} by 10", x);        x * 10    };    if first_number > second_number {        x_number(first_number)    } else {        x_number(second_number)    }}fn main() {    let a = closures_example(9, 125);    println!("{}", a);    let b = closures_example(23, 72);    println!("{}", b);}
cargo run

Image description


Original Link: https://dev.to/mateolafalce/closures-in-rust-closures-en-rust-14o1

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To