Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Value to String in one line #834

Open
hniksic opened this issue Dec 1, 2021 · 2 comments
Open

Convert Value to String in one line #834

hniksic opened this issue Dec 1, 2021 · 2 comments

Comments

@hniksic
Copy link

hniksic commented Dec 1, 2021

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.

@kangalio
Copy link

kangalio commented May 23, 2023

This is implemented in #947. You can check in dtolnay's unholy triage list when he finally gets around reviewing it [man please allow more maintainers to join 😭]

@conradludgate
Copy link

I would prefer a dedicated into_string() rather than the try_into().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants