Stream: cargo-component

Topic: ✔ How to print an enum


view this post on Zulip Christoph Brewing (Nov 24 2023 at 14:46):

Using cargo-component-bindings = "0.5.0" I just added an enum to my .wit:

enum my-type {
    none,
   something,
   sthelse,
  }

When I want to print a variable of type MyType in the code, it says:

error[E0277]: `MyType` doesn't implement `std::fmt::Display`
   --> src/lib.rs:511:21
    |
511 |                     &handle.my_type
    |                     ^^^^^^^^^^^^^^^^ `MyType` cannot be formatted with the default formatter
    |
    = help: the trait `std::fmt::Display` is not implemented for `MyType`

Am I supposed to implement it myself? I ask because when looking at "target/bindings/<project>/bindings.rs", I see at least sth. like impl ::core::fmt::Debug for MyType {. My intuition says, it should not complain. Am I wrong?

view this post on Zulip Jeff Parsons (Nov 24 2023 at 16:39):

The error makes it sound like you have a format string like this: "{}". If you want debug formatting (i.e. using the Debug trait) you need a format string like this: "{:?}".

See also: https://doc.rust-lang.org/std/fmt/trait.Debug.html, https://doc.rust-lang.org/std/fmt/#formatting-traits.

view this post on Zulip Notification Bot (Nov 28 2023 at 07:31):

Christoph Brewing has marked this topic as resolved.


Last updated: Nov 25 2024 at 19:03 UTC