5

I am trying to output the contents of a syn::Expr to the console, but get the following error:

error[E0599]: no method named `to_string` found for type `&syn::Expr` in the current scope
   --> derive/src/lib.rs:165:40
    |
165 |                 println!("Expression: {:#?}", expr.to_string());
    |                                                    ^^^^^^^^^
    |
    = note: the method `to_string` exists but the following trait bounds were not satisfied:
            `syn::Expr : std::string::ToString`
            `&syn::Expr : std::string::ToString`
            `syn::Expr : std::string::ToString`

It is not clear to me what "trait bounds" are or how to satisfy them. Are there any easy ways to output the contents of this variable?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Shawn Tabrizi
  • 12,206
  • 1
  • 38
  • 69
  • 2
    *It is not clear to me what "trait bounds" are* — a quick [search on the internet](https://www.google.com/search?q=rust+%22trait+bounds%22) quickly identifies a chapter in *The Rust Programming Language*. You can then read the [current version of that chapter](https://doc.rust-lang.org/book/second-edition/ch10-02-traits.html). – Shepmaster Oct 08 '18 at 22:52

1 Answers1

11

syn::Expr is documented as implementing the Debug trait, so you use the Debug formatter:

extern crate syn; // 0.15.4

fn example(expr: syn::Expr) {
    println!("{:#?}", expr);
}

However, all Debug implementations in syn are guarded by the Cargo feature extra-traits. So in order to use those Debug impls, you have to specifically enable that feature in your Cargo.toml:

[dependencies]
syn = { version = "0.15", features = ["extra-traits"] }

You can read more about syn's optional Cargo features in their README.


See also:

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Thanks for responding to my dumb question. Unfortunately, I had tried that formatting earlier and got a different error: `^^^^^ 'syn::Expr' cannot be formatted using '{:?}' because it doesn't implement 'std::fmt::Debug'`. It seems like I might be working with a non-standard `syn::Expr`? (note I did use `{:#?}`, but error is missing the hashtag) – Shawn Tabrizi Oct 08 '18 at 23:08
  • 1
    @ShawnTabrizi it's impossible to debug code that we cannot see. Please review what a [MCVE] is and how to produce and provide one. I can say that [the code provided compiles](https://play.rust-lang.org/?gist=05630cb519d72d16f99dd58166a0d74a&version=stable&mode=debug&edition=2015). – Shepmaster Oct 08 '18 at 23:18
  • Thanks Shepmaster, unfortunately I am too unfamiliar with the code base and Rust to reasonably come up with a mcve. That is my bad, and reflects itself in the quality of my post. I appreciate your responses – Shawn Tabrizi Oct 09 '18 at 03:49
  • 2
    @ShawnTabrizi Take another look at this answer. I added a part about the `extra-traits` feature. I hope this helps. – Lukas Kalbertodt Oct 09 '18 at 08:36
  • @LukasKalbertodt Why this doesn't appear in the doc directly ? ["When built, the documentation will then announce whether it was built with the foo feature enabled or disabled, which might be handy"](https://chrismorgan.info/blog/rust-cfg_attr.html) – Stargateur Oct 09 '18 at 08:43
  • @Stargateur it's actually documented [at the top-level of the crate](https://docs.rs/syn/0.15.9/syn/#optional-features). You could file an issue with the library to add that info to each instance of the trait, but that would probably force the maintainers to no longer be able to use `#[derive(...)]`, which would be very annoying. – Shepmaster Oct 09 '18 at 12:48