-2

I have the csvdump.rs code as:

impl EvaluatedTxOut {
    #[inline]
    fn as_csv(&self, txid: &str, index: usize) -> String {
        let address = match self.script.address.clone() {
            Some(address) => address,
            None => {
                debug!(target: "csvdump", "Unable to evaluate address for utxo in txid: {} ({})",
                txid, self.script.pattern);
                String::new()
            }
        };

        // (@txid, indexOut, value, @scriptPubKey, address)
        //format!("{};{};{};{};{}\n",
        format!(
            "{};\n",
            //&txid,
            //&index,
            //&self.out.value,
            //&utils::arr_to_hex(&self.out.script_pubkey),
            &self.script.address
        )
    }
}

I get the following error:

Compiling rusty-blockparser v0.8.2 (C:\Users\allsc\Downloads\Compressed\rusty-blockparser-master\rusty-blockparser-master)

error[E0277]: `Option<std::string::String>` doesn't implement `std::fmt::Display`
--> src\callbacks\csvdump.rs:203:13
    |
203 |             &self.script.address)
    |             ^^^^^^^^^^^^^^^^^^^^ `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: this error originates in the macro `$crate::__export::format_args` which comes from the expansion of the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
warning: `rusty-blockparser` (bin "rusty-blockparser") generated 1 warning
error: could not compile `rusty-blockparser` due to previous error; 1 warning emitted

Can some one please help with this ?

Cerberus
  • 8,879
  • 1
  • 25
  • 40
  • 3
    Perhaps `self.script.address` should just be `address`. – LeopardShark Nov 17 '22 at 22:44
  • 1
    Does [this question on Rusts formatters](https://stackoverflow.com/questions/40100077/what-is-the-difference-between-printlns-format-styles) help? – cafce25 Nov 17 '22 at 22:47
  • 1
    This code does not compile due to the undeclared type and macro, it's better to create a [mre]. The exact answer, however, depends not only on this, but also on some explanation of the goals - what are you trying to output, exactly? – Cerberus Nov 18 '22 at 05:09

1 Answers1

2

To display variables, Rust uses traits. Currently, you're using format!("{}", ...), note the {}. The fact that the brackets are empty means you use the trait named Display. To resolve your issue, either you impl Display for Option<String>, or you use another trait. For debugging prints, you usually want to use format!("{:?}", ...), saying to use the Debug trait. Option implements Debug if and only if the type it depends on does also implement Debug, which is the case of String. Option doesn't implement Display however because this trait is meant to display to the user (and Option doesn't have one way to display that everyone wants).

You can find everything you need about formatting on https://doc.rust-lang.org/std/fmt/index.html

Naeio
  • 1,112
  • 7
  • 21