如何在 Rust 中以最惯用的方式将 Option<T> 转换为 Option<&T>?

Won*_*Hau 4 rust

假设我有一个x类型为 的值Option<T>,如何将其转换为Option<&T>
我尝试使用map

let result = x.map(|x| &x)
                       ^^ Error
Run Code Online (Sandbox Code Playgroud)

但我得到了错误:

cannot return reference to function parameter `x`
returns a reference to data owned by the current function
Run Code Online (Sandbox Code Playgroud)

Won*_*Hau 5

经过一番尝试,我找到了方法,那就是as_ref

参考:https://doc.rust-lang.org/std/option/enum.Option.html#method.as_ref