Rust 如何引用期权中的内容<;T>;?
如何从Rust 如何引用期权中的内容<;T>;?,rust,Rust,如何从选项中提取引用并将其与调用方的特定生命周期一起传递回去 具体地说,我想借用一个框的引用,该框中有一个选项。我想我能做到: impl Bar { fn borrow(&mut self) -> Result<&Box<Foo>, BarErr> { match self.data { Some(e) => Ok(&e), None => Err(BarEr
选项中提取引用
并将其与调用方的特定生命周期一起传递回去
具体地说,我想借用一个框
的引用,该框中有一个选项
。我想我能做到:
impl Bar {
fn borrow(&mut self) -> Result<&Box<Foo>, BarErr> {
match self.data {
Some(e) => Ok(&e),
None => Err(BarErr::Nope),
}
}
}
…但是,这也不行
我遇到问题的完整代码:
#[derive(Debug)]
struct Foo;
#[derive(Debug)]
struct Bar {
data: Option<Box<Foo>>,
}
#[derive(Debug)]
enum BarErr {
Nope,
}
impl Bar {
fn borrow(&mut self) -> Result<&Box<Foo>, BarErr> {
match self.data {
Some(e) => Ok(&e),
None => Err(BarErr::Nope),
}
}
}
#[test]
fn test_create_indirect() {
let mut x = Bar { data: Some(Box::new(Foo)) };
let mut x2 = Bar { data: None };
{
let y = x.borrow();
println!("{:?}", y);
}
{
let z = x2.borrow();
println!("{:?}", z);
}
}
#[派生(调试)]
结构Foo;
#[导出(调试)]
结构条{
数据:选项,
}
#[导出(调试)]
枚举巴雷尔{
不,
}
注入棒{
fn借用(&M自我)->结果{
匹配自我数据{
一些(e)=>正常(&e),
无=>Err(barerrr::Nope),
}
}
}
#[测试]
fn测试\u创建\u间接(){
让mut x=Bar{data:Some(Box::new(Foo))};
设mut x2=Bar{data:None};
{
设y=x.借用();
println!(“{:?}”,y);
}
{
设z=x2.borrow();
println!(“{:?}”,z);
}
}
我很确定我所做的在这里是有效的。首先,你不需要
&mut self
匹配时,应匹配e
作为参考。您正试图返回e
的引用,但它的生存期仅适用于该匹配语句
enum BarErr {
Nope
}
struct Foo;
struct Bar {
data: Option<Box<Foo>>
}
impl Bar {
fn borrow(&self) -> Result<&Foo, BarErr> {
match self.data {
Some(ref x) => Ok(x),
None => Err(BarErr::Nope)
}
}
}
enum BarErr{
不
}
结构Foo;
结构条{
数据:选项
}
注入棒{
fn借用(和自身)->结果{
匹配自我数据{
一些(参考x)=>正常(x),
无=>Err(barerrr::Nope)
}
}
}
从Rust 1.26开始,match工效学允许您编写:
impl Bar {
fn borrow(&mut self) -> Result<&Box<Foo>, BarErr> {
match &self.data {
Some(e) => Ok(e),
None => Err(BarErr::Nope),
}
}
}
对于可变引用有一个伴随方法::
在此之前,我可能会使用map
impl Bar {
fn borrow(&self) -> Result<&Foo, BarErr> {
self.data.as_ref().map(|x| &**x).ok_or(BarErr::Nope)
}
fn borrow_mut(&mut self) -> Result<&mut Foo, BarErr> {
self.data.as_mut().map(|x| &mut **x).ok_or(BarErr::Nope)
}
}
impl条{
fn借用(和自身)->结果{
self.data.as_ref()
}
fn借用\u mut(&mut self)->结果{
self.data.as|mut()
}
}
使用match工效学版本,您可以在线进行映射:
impl Bar {
fn borrow(&mut self) -> Result<&Foo, BarErr> {
match &self.data {
Some(e) => Ok(&**e),
None => Err(BarErr::Nope),
}
}
fn borrow_mut(&mut self) -> Result<&mut Foo, BarErr> {
match &mut self.data {
Some(e) => Ok(&mut **e),
None => Err(BarErr::Nope),
}
}
}
impl条{
fn借用(&M自我)->结果{
匹配和自我数据{
一些(e)=>Ok(&**e),
无=>Err(barerrr::Nope),
}
}
fn借用\u mut(&mut self)->结果{
匹配多个self.data(&M){
一些(e)=>Ok(&mut**e),
无=>Err(barerrr::Nope),
}
}
}
另见:
。而且,我认为代码不需要&mut
。
impl Bar {
fn borrow_mut(&mut self) -> Result<&mut Box<Foo>, BarErr> {
self.data.as_mut().ok_or(BarErr::Nope)
}
}
impl Bar {
fn borrow(&self) -> Result<&Foo, BarErr> {
self.data.as_deref().ok_or(BarErr::Nope)
}
fn borrow_mut(&mut self) -> Result<&mut Foo, BarErr> {
self.data.as_deref_mut().ok_or(BarErr::Nope)
}
}
impl Bar {
fn borrow(&self) -> Result<&Foo, BarErr> {
self.data.as_ref().map(|x| &**x).ok_or(BarErr::Nope)
}
fn borrow_mut(&mut self) -> Result<&mut Foo, BarErr> {
self.data.as_mut().map(|x| &mut **x).ok_or(BarErr::Nope)
}
}
impl Bar {
fn borrow(&mut self) -> Result<&Foo, BarErr> {
match &self.data {
Some(e) => Ok(&**e),
None => Err(BarErr::Nope),
}
}
fn borrow_mut(&mut self) -> Result<&mut Foo, BarErr> {
match &mut self.data {
Some(e) => Ok(&mut **e),
None => Err(BarErr::Nope),
}
}
}