What is this unwrap thing: sometimes it's unwrap sometimes it's unwrap_or

后端 未结 2 1432
我在风中等你
我在风中等你 2020-12-24 00:11

Note The specifics in this question regarding read_line and ~str pertain to a pre-1.0 version of Rust. The general concepts

2条回答
  •  心在旅途
    2020-12-24 00:59

    Note The specifics in this answer regarding read_line and ~str pertain to a pre-1.0 version of Rust. The general concepts about unwrap and unwrap_or remain relevant.

    Because read_line might fail it returns Option<~str>. To get the value out you can use pattern matching or one of the unwrap methods.

    The difference between unwrap and unwrap_or is that unwrap will fail if there is no value (None) but unwrap_or will return the specified default ("nothing" in this case)

提交回复
热议问题