Open In App

Rust – Closers and Type Anonymity

Improve
Improve
Like Article
Like
Save
Share
Report

In Rust, we have Closures and Type Anonymity concepts that serve two purposes. Closures are responsible for enclosing functions in enclosing environments while Type Anonymity bounds unbounded type parameters with traits so that we can specify the types.

Closures:

In Rust, we have the ability to create anonymous functions that take arguments known as closures. Closures are always anonymous, hence we bind them to references. Whenever there is a need to capture functions in an enclosing environment then we specifically use Closures.

The features of closures are:

  • They use || for input variables
  • They can capture outer environment variables too
  • They have an optional {} for single expressions and mandatory for functions

Syntax:

|num| num + x

Example 1:

Rust




//Rust - Closers 
fn main() {
    let mult_one = |num| { 5 * num };
  
println!("The multiplication of 5 and 10 is {}.", mult_one(10));
}


Output:

 

Example 2:

Rust




// Rust - Closers
fn main() {
    let var: i32 = 20;
  
    let print_var = || { println!("variable is: {}", var); 
          
    };
  
    print_var();
}


Output:

 

Explanation:

In this example, we declared a variable named ‘var’ and assigned it to 20. As we know that anonymous functions close the environment therefore this || syntax refers to an anonymous closure that takes no arguments. 

Example 3:

Rust




// Rust - Closers 
fn main() {
    fn func_one(x: i32) -> i32 { x + 10 }
  
    let annotated_closure_variable = |x: i32| -> i32 { x + 5 };
  
    let x = 10;
    println!("function value: {}", func_one(x));
    println!("Value of annotated_closure: {}", annotated_closure_variable(x));
    let var = || 5;
    println!("closure returning var: {}", var());
  
}


Output:

 

Explanation:

In this example, we are implementing closures and functions by incrementing values. In the function func_one, we pass x as an argument that has i32 (32-bit integer value). The value of func_one is incremented by 10. Then, we declare an annoted_closure_variable and their value of x is incremented by 5. Next, we declare a variable x and then call the function and closures respectively. Here, var is a closure.

Type Anonymity:

In Rust, we have a concept of type anonymity. In situations, where closures are defined the Rust compiler creates anonymous structures so that the variables inside are implemented by traits creating. This unknown type generally requires generics. Therefore, to specify type anonymity we bound the variable by traits for most types specifying.

Example 4: 

Rust




Rust Type Anonymity
fn gfg<G>(f: G) where
    G: Fn() {
    f();
}
  
fn main() {
    let var =  10;
    let value = || println!(" Value of var: {}", var);
  
    gfg(value);
}


Output:

 

Explanation:

In this example, ‘G’ is implementing Fn which is a closure that takes no inputs and returns nothing, We have declared a function GFG trait where gfg G: Fn and f() is a function. Now,  we declare a variable ‘var’ and assign a value of 10. Therefore, var is captured into an anonymous type and implemented for Fn to be stored in ‘value’. We are passing value into gfg function trait and the output is shown above.



Last Updated : 13 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads