Help us understand the problem. What is going on with this article?

Rust で fmap

More than 1 year has passed since last update.

型の中に関数を適応させます。

ああこれってHaskellのfmapに似てる〜ってちょっと感動したのでメモです。
堅牢さと簡潔さが両方求めるような状況下で役立ちそうです。

Haskell

コード

main = do 
   print (fmap  (+7) (Just 10)) 
   print (fmap  (+7) Nothing)

結果

Just 17
Nothing

Rust

コード

fn main() {
    println!("{:?}", Some(10).map(|v| v+7));
    println!("{:?}", None::<usize>.map(|v| v+7));
}

結果

Some(17)
None

参考

AtCoder abc118 arzkさんの回答

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away