An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
April 15, 2022 06:27 pm GMT
Original Link: https://dev.to/mateolafalce/closures-in-rust-closures-en-rust-14o1
Closures in rust / Closures en rust
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
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
Original Link: https://dev.to/mateolafalce/closures-in-rust-closures-en-rust-14o1
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To