About clap & Option<String>

in my rust program, i tried to give a default value to variable "name". However the compile tool give me a panic.

error[E0277]: `Option<std::string::String>` doesn't implement `std::fmt::Display`
 --> src/main.rs:6:24
  |
6 |     #[arg(short, long, default_value_t = Some("shaominghao".to_string()))]
  |                        ^^^^^^^^^^^^^^^ `Option<std::string::String>` cannot be formatted with the default formatter
  |
  = help: the trait `std::fmt::Display` is not implemented for `Option<std::string::String>`
  = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
  = note: required because of the requirements on the impl of `ToString` for `Option<std::string::String>`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `oom_monitor` due to previous error

i don't konow why i can't use this default value. Because in the normal case, the varibale's can also be in this

Provide the complete code snippet instead of screenshot, and put it in a codeblock as markdown format.

1 Like

If you give your option type a default value, how is the field ever going to be None? Your option doesn't serve a purpose here, you can make your field be just a String, without the Option.

3 Likes

sorry sir, this is all the code allready, and i will learn how to provide the code in your favorate mode. Please give me a little time.

your replay is very good. If i give a default value, it would can't be none. I can understand your means. But i can't understand why the compile tools give me the error about "format display". Because it could be display.

No, Option<T> doesn't implement Display, even if T does.

1 Like

But if i run the code, i will give a variable such as "hello", and then the program will println! Some("hello"). I give the debug trait to the struct.

Yes, but Debug is a different trait than Display. Your Option implements Debug if T does, but never Display.

1 Like

That doesn't matter.

The Parser from clap needs Display when default_value_t is given.

For example

#[derive(Parser, Debug)]
struct Cli {
    #[arg(default_value_t = String::from("abc"))]
    name2: String,
}

// macro expansion:
...
let arg = arg.default_value({
    static DEFAULT_VALUE: ::std::sync::OnceLock<String> =
        ::std::sync::OnceLock::new();
    let s =
        DEFAULT_VALUE.get_or_init(||
                {
                    let val: String = String::from("abc");
                    ::std::string::ToString::to_string(&val) // Note here.
                });
    let s: &'static str = &*s;
    s
});
...

Note the line ::std::string::ToString::to_string(&val) lies.

ToString trait is automatically implemented for any type which implements the Display trait.

1 Like

thank you very much. i just come back and when i see your replay i understant why i can't do this!

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.