Skip to content

Latest commit

 

History

History
36 lines (35 loc) · 679 Bytes

18.closures-box-iterators.md

File metadata and controls

36 lines (35 loc) · 679 Bytes

Box

  • When we want to allocate something in heap explicitly we use Box
fn main(){
   let a = Box::new(10);
    println!("{}",a);
}

Function as variables

fn main() {
    let incr = |x| x+1;
    let y = 10;
    println!("{}", incr(y));
    let p = || println!("this is the no param closure");
    p();
}

Function in Struct

struct A{
    f: Fn(i32)->i32
}

Function as return from function

  • since each function has it's own stack and to pass something out to stack we need to move the function to heap using Box
fn create()->Box<Fn()> {
    Box::new(move |x| x*x)
}
fn main(){
    let pow = create();
    pow(10);
}