How can we return an anonymous type in Result trait

Hi ,
I try to re implement a java script microservice project ( to rust .
when i try to call an rpc and return value , the remote function result may be anonymous , i write a sample code that i want to run but i got error .
you can see my problem in , please help me how can i pass my values to json api that returns various type .

You can't use the trait Any here. Instead you need some trait that allows the type to be created from a json string. For this you can use serde:

    R: serde::de::DeserializeOwned,

Then when you call it you can specify the value of R in each situation. For more specifics, I'd need to know what web crate you're using.


Hi alice ,
Thanks and i try it now .
may i have a sample code in plyground ?

I think this is the kind of pattern you can use:

extern crate serde;
extern crate serde_json;

use serde::{

pub fn foo<R: DeserializeOwned>() -> Result<R, serde_json::Error> {
    let stuff = r#" { "a": "foo", "b": 32 } "#;

fn main() {
    #[derive(Deserialize, Debug)]
    struct Bar {
        a: String,
        b: i32,
    let a: Bar = foo().unwrap();
    let b: serde_json::Value = foo().unwrap();
1 Like

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.