Matt
Matt

Reputation: 313

create a mutable attribue with a defualt value?

This MWE demonstrates how I can create a struct with an attribute that can be updated inplace. What I would like to do is extend this so that I can call MyStruct::new(None) and get a default value for the .param attribute. I tried numerous things along this line of thought:

    pub fn new(param: Option<&'a mut usize>) -> Result<Self, Error> {
        let tmp = param.unwrap_or(&mut 342);
        Ok(MyStruct { param: tmp })
    }

which, of course, does not work. The error is clear:

error[E0515]: cannot return value referencing temporary value
  --> src/main.rs:11:9
   |
10 |         let tmp = param.unwrap_or(&mut 342);
   |                                        --- temporary value created here
11 |         Ok(MyStruct { param: tmp })
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function

However, I need some help actually resolving it.

Upvotes: 1

Views: 71

Answers (1)

Zeppi
Zeppi

Reputation: 1235

in fact the raison is &mut not implement Clone and Copy trait and "tmp" is owned by the "new" function.

What is the goal tht's "param" is declared mut?

This work

use anyhow::Error;

#[derive(Debug)]
pub struct MyStruct {
    param: usize,
}

impl MyStruct {
    pub fn new(param:  Option<usize>) -> Result<Self, Error> {
        let tmp = param.unwrap_or(342);
        Ok(MyStruct { param: tmp })
    }

    pub fn updater(&mut self, e: usize) {
        self.param = e
    }
}

This also

use anyhow::Error;

#[derive(Debug)]
pub struct MyStruct<'a> {
    param: &'a usize,
}

impl<'a> MyStruct<'a> {
    pub fn new(param: Option<&'a usize>) -> Result<Self, Error> {
        let tmp = param.unwrap_or(&342);
        Ok(MyStruct { param: tmp })
    }

    pub fn updater(&mut self, e: &'a usize) {
        self.param = e
    }
}

fn main() -> Result<(), Error> {
    let mut x = MyStruct::new(None)?;
    println!("param = {:?}", &x.param);
    
    x.updater(&14);
    println!("param = {:?}", &x.param);
    
    Ok(())
}

Upvotes: 1

Related Questions