Michał Zabielski
Michał Zabielski

Reputation: 434

Can the ? operator be defined for types other than Option and Result?

The question mark operator (?) is defined for Option and Result types. Is it possible to overload it for some custom type? I haven't found any trait that might guide this behavior.

Upvotes: 9

Views: 2435

Answers (1)

Alexey S. Larionov
Alexey S. Larionov

Reputation: 7927

The trait you're looking for is called std::ops::Carrier. However as you can see it's experimental and it caused lots of discussions on how to implement it (I believe it's experimental for a few years already).

So technically you may use it, but you better don't rely on it as its look might be changed or it can be removed for good.

Upvotes: 5

Related Questions