Skip to content

Convert Value to String in one line #834

Open
@hniksic

Description

@hniksic

Is there a convenience method to convert owned value to owned String, like an owned version of as_str(). For example:

let v: Value = json!("foo"); // in my case some_map.remove(some_key)
let s: String = v.try_into().unwrap();

The above doesn't compile, and there is no Value::into_string() or similar. As far as I can tell, the only way to extract the string is by matching:

let s = match v {
    Value::String(s) => s,
    _ => panic!("expected String"),
};

I need this kind of thing fairly often and I wonder if there's a more succinct way to write the above. Of course, I can write a utility function that does it, but would prefer to use a standard API offered by the crate if possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions