How to use anonymous function (closures) in Rust

2 Answers

0 votes
fn main() {
    let runthis = || {
        println!("Anonymous Function");
    };
    
    runthis();
}





/*
run:

Anonymous Function

*/

 



answered Feb 8, 2023 by avibootz
0 votes
fn main() {
    let add = |a: u32, b: u32| -> u32 {
        println!("Anonymous Function");
        return a + b;
    };
    println!("{}", add(34, 98));
}





/*
run:

Anonymous Function
132

*/

 



answered Feb 8, 2023 by avibootz

Related questions

...