You have a few options, but I'd go for one of these:
Into<bool> (From<Example>)
If your trait conceptually represents a bool, but maybe with some extra metadata, you can implement From<Example> for bool:
impl From<Example> for bool { fn from(e: Example) { // perform the conversion } }
Then you can:
fn main() { let x = Example { /* ... */ }; if x.into() { // ... } }
Custom method
If your type doesn't really represent a boolean value, I'd usually go for an explicit method:
impl Example { fn has_property() -> bool { /* ... */ } }
This makes it more obvious what the intent is, for example, if you implemented From<User> for bool:
fn main() { let user = User { /* ... */ }; if user.into() { // when does this code get run?? } // compared to if user.logged_in() { // much clearer } }
Exampleis not a boolean type, it is used as such. So the real conversion is hidden, and from the source it is not clear what is meant. :-(falsemeans empty container andtruemeans non-empty? The idiomatic way to do this in Rust depends on your actual use case.if keven mean? You're better off implementing a specific method that returns a boolean and calling that -- egif k.has_data()