Prevent the String() around a String

I'm creating UUIDs and put them into a vector. I want them as String literal like b9c5f6f3-0c15-460d-b597-2e3496d383b6. Instead I always get this as the output String("b9c5f6f3-0c15-460d-b597-2e3496d383b6"). Why does that happen when I'm using to_string() and what do I have to do to fix this?

pub fn generate_user_uuid(students_num: i32) -> Value {
    let mut uuids: Vec<String> = Vec::new();

    info!("Generating UUIDs for a unique signup process...");

    for _i in 0..students_num {
        let id: Uuid = Uuid::new_v4();


    debug!("UUIDS: {:#?}", uuids);

    let json: Value = json!({
        "uuids": uuids.iter().map(|f| &**f).collect::<Vec<&str>>()
    let json_str: String = serde_json::to_string(&json.get("uuids")).unwrap();

        .set_str("uuids", &json_str)
        .expect("Failed to save signup URLs into the cache");


Implement std::fmt::Debug (and/or Display) yourself instead of deriving it.

You could just call str's impl, say. (Sorry for no example, on mobile.)

((Also, the particulars of the derived output aren't guaranteed; if you care, don't derive.))

Can you give me an example later?

I was going to give it a go on mobile, at which point I found we're not talking about a new type like I thought we were. So probably ignore me for the moment, sorry.

I'll return to this later if no one else beats me to it, with an example and/or correction.

1 Like

Can you explain further which line is producing the problematic output in your snippet? I don’t think the debug! statement is producing the output you point out.

Here is a simplified version of the snippet in the original post:

use uuid; // 1.5.0

fn main() {
    let id = uuid::Uuid::new_v4();
    let ids = vec![id.to_string()];
    println!("{:#?}", ids);




Instead, I suspect a serde_json::Value is being printed in some other code, not part of the snippet in the original post.


use uuid; // 1.5.0
use serde_json; // 1.0.108

fn main() {
    let id = uuid::Uuid::new_v4();
    let value = serde_json::Value::String(id.to_string());
    println!("{:#?}", value);




A serde_json::Value can be many other things than a String. If you want to print the JSON value as a string, you need to extract the inner value using something like as_str or pattern matching. The value may not be a string, so you have to handle that too. You may want to modify the code to not work with a serde_json::Value if you only work with a collection of strings.


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.