Rust - Functions With Closure as Parameter Last Updated : 06 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Rust, there are anonymous functions that accept closure as parameters. In closures, we are generally not required to annotate types of parameters or return values as opposed to functions. Closures are stored in variables and are used without requiring to name them and exposing them to users of the code libraries. Closures are generally used for inferring the variable types. Type annotations are required for applying functions as types are generally part of an explicit interface that is exposed to the user's code library.While accepting closures as input parameters, the closure's type is annotated with traits.Before proceeding with the example, we need to know some terms related to traits:Fn refers to closures that use captured value by referenceFnOnce refers to the closure that is used by capture by value.Example 1: Rust // Rust code for Functions With Closure as Parameter fn gfg<G>(f: G) where G: FnOnce() { f(); } #[allow(dead_code)] fn apply_function<G>(f: G) -> i32 where G: Fn(i32) -> i32 { f(3) } fn main() { use std::mem; let string_one = "Hackathons"; let mut string_two = "GFG ".to_owned(); let value = || { // `greeting` is by reference: requires `Fn`. println!("GeeksforGeeks {}.", string_one); string_two.push_str("Courses"); println!("Learning Coding from {}.", string_two); mem::drop(string_two); }; gfg(value); } Output: Explanation:In this example, we have declared Fn and FnOnce as the two traits. Here, <G> refers to G which is a Generic type parameter. gfg is declared as a function that takes a closure argument and calls it. The closure G: FnOne does not take any input parameters and therefore does not return anything also at the same time. The function apply_function takes a closure of a 32-bit integer and returns it as well. The std::mem is used for the management of memory and it provides basic functions that deals with memory. We have declared two strings string_one and string_two (which are mutable). string_two is mutable in nature and it is a noncopy type and the to_owned is a borrowed concept that is responsible for borrowing data. Now, we have declared a variable named 'value' that captures the two variables string_one by reference and string_two by value. If we see the function closely, we can see that string_one requires a reference named 'Fn' and due to mutation string_two requires FnMit as a mutable reference so that it is captured. After our job is done, we drop 'string_two' using the std::mem so that string_two is captured by value which in turn requires Fn) once again. Lastly, call the function gfg and pass 'value' as a closure. Comment More infoAdvertise with us Next Article Rust - Functions With Closure as Parameter S sayanc170 Follow Improve Article Tags : Technical Scripter Rust Technical Scripter 2022 Rust functions Similar Reads Rust - Closure as an Input Parameter In Rust, Closures can be taken as input for the parameters. Closures are functions that wrap up functions for reusable purposes, which help capture variables in the enclosing environment. Closures are closed over when used in functions and for a single expression elimination ({ }) is used. The closu 3 min read Rust - Generic Function In Rust, Generic functions are very useful. Generic make code more flexible and provide more functionality to the callers of the function. It prevents code duplication as it is not required to define different functions of different types. Generics are specified in the signature of function where we 3 min read What is the practical use for a closure in JavaScript? In JavaScript, closures are defined as inner functions that have access to variables and parameters of outer function even after the outer function has returned. The below examples show the practical use of closures: Example: In this example, a variable mult is defined that is local to the function 2 min read Rust - Concept of Capturing Variables with Closers In Rust, we can capture variables via Closures. Closures are anonymous functions that help us in saving variables or pass arguments to other functions. Closures can be created from one place and then can be called anywhere and they can capture values from their defined scope. While capturing variabl 2 min read Calling Functions in Swift Functions are independent lumps of code that play out a particular undertaking. You can give a name to the function that recognizes what it does, and also the name is utilized to "call" the function to play out its errand when required. Or we can say that a function is a bunch of orders/explanations 6 min read Like