提交 9307917d 编写于 作者: J Jonathan Strong

Adds language to the documentation for `Option` and `Result` suggesting

the use of lazily evaluated alternatives when appropriate. These
comments are intended to echo a clippy lint on the same topic (see
https://rust-lang-nursery.github.io/rust-clippy/master/index.html#or_fun_call)
上级 9f3b0911
......@@ -338,6 +338,10 @@ pub fn unwrap(self) -> T {
/// Returns the contained value or a default.
///
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
/// the result of a function call, it is recommended to use `unwrap_or_else`,
/// which is lazily evaluated.
///
/// # Examples
///
/// ```
......@@ -451,6 +455,10 @@ pub fn map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(self, default: D, f:
/// Transforms the `Option<T>` into a [`Result<T, E>`], mapping [`Some(v)`] to
/// [`Ok(v)`] and [`None`] to [`Err(err)`].
///
/// Arguments passed to `ok_or` are eagerly evaluated; if you are passing the
/// result of a function call, it is recommended to use `ok_or_else`, which is
/// lazily evaluated.
///
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
/// [`Ok(v)`]: ../../std/result/enum.Result.html#variant.Ok
/// [`Err(err)`]: ../../std/result/enum.Result.html#variant.Err
......@@ -609,6 +617,10 @@ pub fn and_then<U, F: FnOnce(T) -> Option<U>>(self, f: F) -> Option<U> {
/// Returns the option if it contains a value, otherwise returns `optb`.
///
/// Arguments passed to `or` are eagerly evaluated; if you are passing the
/// result of a function call, it is recommended to use `or_else`, which is
/// lazily evaluated.
///
/// # Examples
///
/// ```
......
......@@ -625,6 +625,10 @@ pub fn and_then<U, F: FnOnce(T) -> Result<U, E>>(self, op: F) -> Result<U, E> {
/// Returns `res` if the result is [`Err`], otherwise returns the [`Ok`] value of `self`.
///
/// Arguments passed to `or` are eagerly evaluated; if you are passing the
/// result of a function call, it is recommended to use `or_else`, which is
/// lazily evaluated.
///
/// [`Ok`]: enum.Result.html#variant.Ok
/// [`Err`]: enum.Result.html#variant.Err
///
......@@ -690,6 +694,10 @@ pub fn or_else<F, O: FnOnce(E) -> Result<T, F>>(self, op: O) -> Result<T, F> {
/// Unwraps a result, yielding the content of an [`Ok`].
/// Else, it returns `optb`.
///
/// Arguments passed to `unwrap_or` are eagerly evaluated; if you are passing
/// the result of a function call, it is recommended to use `unwrap_or_else`,
/// which is lazily evaluated.
///
/// [`Ok`]: enum.Result.html#variant.Ok
/// [`Err`]: enum.Result.html#variant.Err
///
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册