fn owned_context() {
    let obj1 = MyStruct::new("Hello", 15);
    let obj2 = MyStruct::new("More Text", 10);

    // "obj1" is owned by the closure.
    let closure4 = |x: &MyStruct| {
        obj1.destructor();
        x.get_number()
    };

    // Does not compile:
    // is_fn(closure4);
    // is_Fn(&closure4);
    // is_FnMut(&closure4);

    // Compiles successfully:
    is_FnOnce(&closure4);

    assert_eq!(closure4(&obj2), 10);
    // We can't call "closure4" twice...
    // assert_eq!(closure4(&obj2), 10);             // ERROR
    // We can't borrow "obj1" mutably or immutably
    // assert_eq!(obj1.get_number(), 15);           // ERROR
    // obj1.inc_number();                           // ERROR

    struct Context(MyStruct);
    let obj1 = MyStruct::new("Hello", 15);
    let obj2 = MyStruct::new("More Text", 10);
    let ctx = Context(obj1);

    // "obj1" is owned by the closure
    fn func4(context: Context, x: &MyStruct) -> u32 {
        context.0.destructor();
        x.get_number()
    };

    assert_eq!(func4(ctx, &obj2), 10);
    // We can't call func4 twice...
    // assert_eq!(func4(ctx, &obj2), 10);           // ERROR
    // We can't borrow obj1 mutably or immutably
    // assert_eq!(obj1.get_number(), 15);           // ERROR
    // obj1.inc_number();                           // ERROR
}
// In this example, we can only call the function once. Once we have called it the first
// time, we have destroyed obj1, so it no longer exists for the second call.
In this example, we can only call the function once. Once we have called it the first
time, we have destroyed obj1, so it no longer exists for the second call.
In this example we can only call the function once . Once we have called it the first time we have destroyed obj1 so it no longer exists for the second call .
fn owned_context() {
    let obj1 = MyStruct::new("Hello", 15);
    let obj2 = MyStruct::new("More Text", 10);
    // "obj1" is owned by the closure.
    let closure4 = |x: &MyStruct| {
        obj1.destructor();
        x.get_number()
    };
    // Does not compile:
    // is_fn(closure4);
    // is_Fn(&closure4);
    // is_FnMut(&closure4);
    // Compiles successfully:
    is_FnOnce(&closure4);
    assert_eq!(closure4(&obj2), 10);
    // We can't call "closure4" twice...
    // assert_eq!(closure4(&obj2), 10);             // ERROR
    // We can't borrow "obj1" mutably or immutably
    // assert_eq!(obj1.get_number(), 15);           // ERROR
    // obj1.inc_number();                           // ERROR
    struct Context(MyStruct);
    let obj1 = MyStruct::new("Hello", 15);
    let obj2 = MyStruct::new("More Text", 10);
    let ctx = Context(obj1);
    // "obj1" is owned by the closure
    fn func4(context: Context, x: &MyStruct) -> u32 {
        context.0.destructor();
        x.get_number()
    };
    assert_eq!(func4(ctx, &obj2), 10);
    // We can't call func4 twice...
    // assert_eq!(func4(ctx, &obj2), 10);           // ERROR
    // We can't borrow obj1 mutably or immutably
    // assert_eq!(obj1.get_number(), 15);           // ERROR
    // obj1.inc_number();                           // ERROR
}
fn owned_context ( ) { let obj1 = MyStruct :: new ( " " , 15 ) ; let obj2 = MyStruct :: new ( " " , 10 ) ; // "obj1" is owned by the closure. let closure4 = | x : & MyStruct | { obj1 . destructor ( ) ; x . get_number ( ) } ; // Does not compile: // is_fn(closure4); // is_Fn(&closure4); // is_FnMut(&closure4); // Compiles successfully: is_FnOnce ( & closure4 ) ; assert_eq ! ( closure4 ( obj2 ) 10 ) ; // We can't call "closure4" twice... // assert_eq!(closure4(&obj2), 10);             // ERROR // We can't borrow "obj1" mutably or immutably // assert_eq!(obj1.get_number(), 15);           // ERROR // obj1.inc_number();                           // ERROR struct Context ( MyStruct ) ; let obj1 = MyStruct :: new ( " " , 15 ) ; let obj2 = MyStruct :: new ( " " , 10 ) ; let ctx = Context ( obj1 ) ; // "obj1" is owned by the closure fn func4 ( context : Context , x : & MyStruct ) -> u32 { context . 0 . destructor ( ) ; x . get_number ( ) } ; assert_eq ! ( func4 ( ctx obj2 ) 10 ) ; // We can't call func4 twice... // assert_eq!(func4(ctx, &obj2), 10);           // ERROR // We can't borrow obj1 mutably or immutably // assert_eq!(obj1.get_number(), 15);           // ERROR // obj1.inc_number();                           // ERROR }
In this example, we can only call the function once.
In this example we can only call the function once .