News for hackers. Stuff that matters.
Re:Lambda expression comparison between C++11, C++14 and C++17 by steveklabnik on Saturday May 14, 2016 @06:13PM (#11698292)
It's a trade off more than a necessity. For example, Rust doesn't have explicit capture lists, and if you want explicit control, you make new bindings and capture those. You almost never need to do this in Rust, so it's optimized for that case; I haven't written many closures in C++, so I can't say as much about the frequency there.

To make this more concrete:

    let s = String::from("s");
    
    let closure = || {
        println!("s is: {}", s);
    };
    
    closure();
If you wanted to capture s in a different way:

    let s = String::from("s");
    
    let s1 = &s;
    let closure = || {
        println!("s1 is: {}", s1);
    };
    
    closure();
No capture list needed, same control.
Re:Lambda expression comparison between C++11, C++14 and C++17 by kibwen on Saturday May 14, 2016 @06:18PM (#11698308)
Rust doesn't have explicit capture lists, but it does have the `move` modifier on closures which is like C++'s `[=]`.

Strictly speaking, Rust probably could have gotten away with having neither the `move` modifier nor capture clauses at all, but it would have had wide-ranging implications on the ergonomics and capabilty of closures.

Re:Lambda expression comparison between C++11, C++14 and C++17 by hendzen on Saturday May 14, 2016 @06:20PM (#11698317)
How would you do the equivalent of this in Rust?

  auto on_heap = std::make_unique<MyType>(...);

  function_that_accepts_lambda([obj = std::move(on_heap)]() {
     obj->bar(...);
  })
Re:Lambda expression comparison between C++11, C++14 and C++17 by steveklabnik on Saturday May 14, 2016 @06:22PM (#11698323)

    let on_heap = Box::new(...);

    function_that_accepts_lambda(move || {
        on_heap.bar();
    });
This is sort of what kibwen was mentioning: move is a single annotation that overrides everything to capture by value rather than have it inferred.
Re:Lambda expression comparison between C++11, C++14 and C++17 by hendzen on Saturday May 14, 2016 @06:39PM (#11698383)
What if you want to move some things, but copy others?

e.g.

  auto shared = std::make_shared<MySharedType>(...);
  auto unique = std::make_unique<MyOwnedType>(...);

  function_that_accepts_lambda([shared, u = std::move(unique)] {
      shared->foo(...); u->bar(...);
  });

  // Outer scope can still use shared.
  shared->foo(....);
Re:Lambda expression comparison between C++11, C++14 and C++17 by steveklabnik on Saturday May 14, 2016 @07:03PM (#11698461)
I am 99% sure this is identical:

    let on_heap = Box::new(...);
    let shared = Arc::new(...);

    let s = shared.clone();
    function_that_accepts_lambda(move || {
        on_heap.bar();
        s.foo();
    });
We have to make the extra s binding.

Also, my sibling is correct that Copy types will just be copied, not moved.

Re:Lambda expression comparison between C++11, C++14 and C++17 by vvanders on Saturday May 14, 2016 @06:46PM (#11698403)
If the type implements Copy they'll be implicitly copied when moved into the Rust closure(I think). Or you can declare a scope var and clone() manually.