Option

Option<T> の使用方法についてはすでにいくつか見てきましたが、これは型 T の値を格納するか、何も格納しません。たとえば、String::findOption<usize> を返します。

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Speaker Notes

This slide should take about 10 minutes.
  • Option is widely used, not just in the standard library.

  • unwrapOption 内の値を返すか、パニックになります。expect も同様ですが、エラーメッセージを受け取ります。

    • None でパニックになる場合もありますが、「誤って」None のチェックを忘れることはありません。
    • 何かを一緒にハッキングする場合は、あちこちで unwrap/expect を行うのが一般的ですが、本番環境のコードは通常、None をより適切に処理します。
  • The “niche optimization” means that Option<T> often has the same size in memory as T, if there is some representation that is not a valid value of T. For example, a reference cannot be NULL, so Option<&T> automatically uses NULL to represent the None variant, and thus can be stored in the same memory as &T.