November 2017
Beginner
308 pages
8h 32m
English
As threads within Rust use a return from a closure, it makes sense for us to consider that it is entirely possible to return a closure. However, returning a closure is not as straightforward as you'd expect.
Let's consider a normal function first:
fn add_five(x : i32) -> i32
{
return x + 5;
}
fn main()
{
let test = add_five(5);
println!("{}", test);
}
This will output the value 10. It's not rocket science. Let's change this to a closure:
fn add_five_closure() ->(Fn(i32)->i32)
{
let num = 5;
|x| x + num
}
fn main()
{
let test = add_five_closure();
let f = test(5);
println!("{}", f);
}
When we run this, though, we don't get the expected answer of ...
Read now
Unlock full access