Can anyone point me to the Syntax in Rust for implicit closure parameter? I know I have seen it in the forum before, but can't find it .
In Scala, it's used like this: someSeq.map(_.id)
and someSeq.map(MyCaseClass(_))
.
Can anyone point me to the Syntax in Rust for implicit closure parameter? I know I have seen it in the forum before, but can't find it .
In Scala, it's used like this: someSeq.map(_.id)
and someSeq.map(MyCaseClass(_))
.
You can ignore a closure parameter, and, it being a closure, you can close over free variables from the closure's lexical environment.
But there is no such thing as an implicit parameter in Rust.
But given the code samples, it seems to me you just want to map over a collection. The Rust equivalents would be roughly:
let ids: <TYPE> = some_seq.iter().map(|x| x.id).collect();
let values: <TYPE> = some_seq.iter().map(MyWrapperType).collect();
A couple of notes:
Vec<Id>
MyWrapperType
is a newtype, approximately defined as struct MyWrapperType(pub X);
if some_seq
is a collection of elements of type X
.Rust can usually infer the type within the collection in this pattern.
let v: Vec<_> = other.iter().map(f).collect();
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.