3 ms·
if let is such a nice way to express this if let Some(x) = map.get(foo) { bob(x) } One subtle but noteworthy thing is that the idiom only ment
by im3w1l 4y ago
if let is such a nice way to express this
if let Some(x) = map.get(foo) {
bob(x)
}
One subtle but noteworthy thing is that the idiom only mentions map by name once, which is nice if you have a very long map name, and prevents copy paste bugs where you only update one of the two mentions.
- olliej 4y agoThis pattern exists in C++ as well. The specific issue here is that all these STL APIs are in terms of C++'s iterator model, and can't be replaced with a more modern "optional" style that allows this cleaner coding style :-/
- paulddraper 4y agoScala: for (x <- map.get(foo)) { bob(x) } // or map.get(foo).foreach(bob) // or map.get(foo) match { case Some(x) => bob(x) }