LoginSignup
1
0

More than 3 years have passed since last update.

Rust で fmap

Last updated at Posted at 2019-08-23

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

ああこれって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さんの回答

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0