# How to return Result\<ok, error\> in Rust (edited)

**URL:** <https://forum.dfinity.org/t/how-to-return-result-ok-error-in-rust-edited/5744>\
**Category:** Developers\
**Created:** [July 6, 2021, 3:52pm UTC](https://forum.dfinity.org/t/how-to-return-result-ok-error-in-rust-edited/5744 "2021-07-06T15:52:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rem.codes](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rem.codes/32/40260_2.png) [@rem.codes](https://forum.dfinity.org/u/rem.codes)\
**Post date:** [July 6, 2021, 3:52pm UTC](https://forum.dfinity.org/t/how-to-return-result-ok-error-in-rust-edited/5744/1 "2021-07-06T15:52:34Z")

</div>

I’m trying to fix errors inside my Rust canister in the following function

#[update]  
fn register(add\_user: AddUser) → Result\<User, String\> {  
let principal = ic\_cdk::caller();  
let user\_device\_store = storage::get\_mut::();  
if !user\_device\_store.contains\_key(&principal) {  
return Err(“This device is already registered”.to\_string());  
} else {  
let user\_store = storage::get\_mut::();  
let new\_user = User {  
id: 0,  
email: add\_user.email,  
username: add\_user.username,  
image: “”.to\_string(),  
};

```
user_device_store.insert(principal, 0);
user_store.insert(0, new_user.clone());
return Ok(new_user.clone());

```

}  
}

my .did file  
type Result = variant {  
err: text;  
ok: user;  
};

type add\_user = record {  
email: text;  
username: text;  
};

type user = record {  
id: nat32;  
email: text;  
username: text;  
image: text;  
};

service : {  
“get\_users”: () → (vec user) query;  
“register”: (add\_user) → (Result);  
}

But when i call the function from the frontend i get the following console error  
Error: Cannot find field hash _3456837_  
at VariantClass.decodeValue (idl.ts:1018)  
at idl.ts:1541  
at Array.map ()  
at Object.decode2 (idl.ts:1540)  
at decodeReturnValue (actor.ts:285)  
at caller (actor.ts:363)  
at async register (gateway.ts:10)  
the dfx console doensn’t log anything. If i check the example projects the Result\<\> isn’t used anywhere exept for the guard function in the asset\_storage project.

Is is even possible to give this response?

---

<div class="post-metadata">

**Author:** ![GLdev](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@GLdev](https://forum.dfinity.org/u/GLdev)\
**Post date:** [September 24, 2021, 7:18am UTC](https://forum.dfinity.org/t/how-to-return-result-ok-error-in-rust-edited/5744/3 "2021-09-24T07:18:43Z")

</div>

Hey, I got the same issues, with similar code. Couldn’t get it to work on the JS side, getting the “cannot find field hash” errors for both Ok() and Err() returns…

As a workaround I used this as did:

```auto
type Result = record {
    "err": text;
    "ok": bool;
};

service : {
        "update": (text) -> (Result);
}

```

and this for rust:

```auto
#[derive(Clone, Debug, Default, CandidType, Deserialize)]
struct Res {
    pub ok: bool,
    pub err: String,
}

fn update(name: String) -> Res {
    let principal_id = ic_cdk::caller();
    let id_store = storage::get_mut::<IdStore>();
    let profile_store = storage::get_mut::<ProfileStore>();

    let mut response = Res::default();

    if id_store.contains_key(&name) {
        response.ok = false;
        response.err = [...] <--- cant paste quotes but text goes here

        return response;
    }

    let mut profile = Profile::default();
    profile.name = name.clone();

    id_store.insert(name.clone(), principal_id.clone());
    profile_store.insert(principal_id, profile);

    response.ok = true;
    response
}

```
