Pass any Struct into Function

Hi,

I am writing some reflection software for a wider project with a rust backend. Part of that is making an Object to JSON library. I know there are some crates that already do that, but I want to make as much of this by my own hand as I can.
My question is: is there some way to pass any struct to be made into JSON?
I've tried something like this:

pub struct JsonFile<Struct>{
    // other types not important to this context
    object: Struct,
}

Any help would be greatly appreciated, Thanks!

Do you mean pass into a function?

fn f<T: your traits>(obj: &T) {
   ...
}

You should read up on Generic Types and Traits

1 Like

I was meaning in a struct, My Bad

structs can also receive generic parameters:

struct MyStruct<T> {
 my_field: T
}
1 Like