News for hackers. Stuff that matters.
Re:Rust 1.50 by k__ on Thursday February 11, 2021 @11:18AM (#26103314)
Could you give a before/after example?
Re:Rust 1.50 by Fiahil on Thursday February 11, 2021 @11:27AM (#26103466)
Before:

if predicate() { Some(thing) } else { None }

After:

predicate().then(|| thing)

Re:Rust 1.50 by Shadonototro on Thursday February 11, 2021 @12:02PM (#26104038)
i prefer the Before one, it is clear what it does, it's branching, common, even in plain english or any latin language

the second, that magic is not clear, i'll keep using the Before personally

Re:Rust 1.50 by momothereal on Thursday February 11, 2021 @12:24PM (#26104361)
IMO it depends if you're already in a chaining context. I wouldn't use it in a simple if-else branch, but it also removes the need to use if-else when you're already in a 10-function long chain.

Before:

if a.b().c().d().e().f() { Some(g().h().i()) } else { None }

Now:

a().b().c().d().e().f().then(|| g().h().i())

Even better if you have multiple boolean returns in the chain.

Re:Rust 1.50 by csomar on Thursday February 11, 2021 @12:33PM (#26104504)
The naming can be confusing since "then" is usually used for async operations.
Re:Rust 1.50 by steveklabnik on Thursday February 11, 2021 @12:39PM (#26104600)
That ship had already sailed in Rust https://doc.rust-lang.org/stable/std/?search=then
Re:Rust 1.50 by hobofan on Thursday February 11, 2021 @12:41PM (#26104633)
Option and Result already have a `and_then` in addition to the futures methods you mention, so it should just always be assumed that then/and_then have slightly different meanings depending on the context.
Re:Rust 1.50 by alfiedotwtf on Friday February 12, 2021 @02:28AM (#26111423)
I used to like explicit if/then and using match, but chaining really does reduce code. It can sometimes take a while to golf it down, but damn is it satisfying when it’s all so compact. Followed by a `cargo fmt` and it feels way more elegant than branching
Re:Rust 1.50 by trevor-e on Thursday February 11, 2021 @03:36PM (#26106775)
Can you also please explain what the "||" is doing? That looks really weird to me coming from Swift and don't see it in the docs.

edit: thanks all, should have known it was a lambda :D, in Swift we can omit this. My first thought seeing "||" was some type of OR logic since it's in the context of if/else

Re:Rust 1.50 by Sharlin on Thursday February 11, 2021 @03:52PM (#26106991)
Rust lambdas look like this:

    |ar,gs| expr
So `|| expr` is just a lambda function that takes no arguments and evaluates to `expr`.
Re:Rust 1.50 by varajelle on Thursday February 11, 2021 @03:52PM (#26106987)
It is a lambda function with no parameters.

The argument of then() is a function which is evaluated only if the bool is true.

Re:Rust 1.50 by laszlokorte on Thursday February 11, 2021 @03:53PM (#26107002)
empty parameter list of a closure.

rust: |x| x+x

swift: {x in x+x}

rust: || 42

swift: { 42 }

Re:Rust 1.50 by oblio on Thursday February 11, 2021 @03:21PM (#26106612)
Rust is awesome but I swear the language developer have no aesthetic eye. I mean, they do have one, but it definitely isn't that of Elvis (and definitely not that of Mort).

https://web.archive.org/web/20080218051638/http://www.nikhil...