Howto write a From<> function for own struct?

Hi I'm in need to write a From for MyStruct.

with

pub MyStruct{
    a: u32,
    b: bool
}

but i actually get no good atampt for the

impl From<OwnedValue> for MyStruct{
     fn from(value: OwnedValue) -> Self{
     // ??? 
     }
}

I tried it with serailize and unsearialize but i faild.

some ideas would be soo helpful.
thx in advance. Karl

You have to specify the 'from what'.
Something like this.

#[derive(Debug)]
struct MyStruct
{
    a: u32,
    b: bool
}

impl From<u32> for MyStruct
{
    fn from(value: u32) -> Self
    {
       Self {a: value, b: value == 42 }
    }
}

fn main()
{
    let s = MyStruct::from(42);
    println!("{:?}", s);
}
4 Likes

I formatted the code in your post. In the future please format your code to make it more readable for those trying to help you:
https://users.rust-lang.org/t/forum-code-formatting-and-syntax-highlighting/42214

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.