How to implement the trait `std::ops::Try` for any struct?

Consider I have created a struct like mentioned bellow

struct AppKey {
 name: &str,
 key: &str
}

How do I implement the trait std::ops::Try that is not implemented for AppKey

Why do you need to implement Try for it? That trait is only meant for types that contain successes or failures (like Option and Result.)

2 Likes

The first thing to realize is that Try is an unstable feature, and it requires nightly if you wish to implement it for your own type.

The second thing to realize is that it doesn't appear to make much sense to implement Try for your type. What are the ok and error cases for that struct?

I assume you got the idea of implementing Try from an error message about using ?. Perhaps you should tell us about this case, so we can point you towards a better appraoch.

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.