| 343 | // ----------------------- |
| 344 | |
| 345 | fn function_closures(){ |
| 346 | // Also known as anonymous functions or lambda functions. |
| 347 | // The data types of arguments and returns are optional |
| 348 | |
| 349 | // Other characteristics of closures include: |
| 350 | // - using || instead of () around input variables. |
| 351 | // - optional body delimitation {} for a single line expression (mandatory otherwise). |
| 352 | // - the ability to capture the outer environment variables. |
| 353 | // - Using move before vertical pipes forces closure to take ownership of captured variables: let closure = move || {} |
| 354 | |
| 355 | // - Fn: Represents closures that can be called multiple times without mutating their environment. |
| 356 | |
| 357 | |
| 358 | |
| 359 | // - FnMut: Represents closures that can mutate their environment when called. |
| 360 | // fn call_fnmut<F>(mut f: F) |
| 361 | // where |
| 362 | // F: FnMut(), |
| 363 | // { |
| 364 | // f(); |
| 365 | // f(); // FnMut can be called multiple times |
| 366 | // } |
| 367 | |
| 368 | // - FnOnce: Represents closures that can be called at least once but might consume variables |
| 369 | // they capture, meaning they might not be callable more than once. |
| 370 | // fn call_fnonce<F>(f: F) |
| 371 | // where |
| 372 | // F: FnOnce(), |
| 373 | // { |
| 374 | // f(); // FnOnce can be called at least once |
| 375 | // } |
| 376 | |
| 377 | println!("\n======================================================================"); |
| 378 | println!("* FUNCTION CLOSURES: (Also known as anonymous functions or lambda functions)"); |
| 379 | |
| 380 | // --------------------------------------------------------------------------------------- |
| 381 | // With optional type declarations of input and return types |
| 382 | println!("\n1. With optional type declarations of input and return types"); |
| 383 | // --------------------------------------------------------------------------------------- |
| 384 | let square = |i: i32| -> i32 { // Input parameters are passed inside | | and expression body is wrapped within { } |
| 385 | i * i |
| 386 | }; |
| 387 | println!("\tlet square = |i: i32| -> i32 {{ i * i }}; // Input parameters are passed inside | | and expression body is wrapped within {{ }}"); |
| 388 | println!("\tsquare(2): {}", square(2)); |
| 389 | |
| 390 | // --------------------------------------------------------------------------------------- |
| 391 | // Without type declarations of input and return types |
| 392 | println!("\n2. Without type declarations of input and return types"); |
| 393 | // --------------------------------------------------------------------------------------- |
| 394 | let square = |i| i * i; // { } are optional for single-lined closures |
| 395 | println!("\tlet square = |i| i * i; // {{ }} are optional for single-lined closures"); |
| 396 | println!("\tsquare(2): {}", square(2)); |
| 397 | |
| 398 | // --------------------------------------------------------------------------------------- |
| 399 | // With optional type declarations; Creating and calling together |
| 400 | println!("\n3. With optional type declarations; Creating and calling together"); |
| 401 | // --------------------------------------------------------------------------------------- |
| 402 | let x_square = |i: i32| -> i32 { i * i }(2); // { } are mandatory while creating and calling same time. |