Implementing the strategy pattern in Rust

I came nearly to the same conclusion: playground.

The main difference is that the Bold and Strike trait are declared inside the function bold() and strike(), and TextFormatter is generic only on TextFormatterImpl.

Thanks a lot, I'm finally happy with my solution.

✓ easy to use and hard/impossible to misuse
✓ easy to implement
✓ zero-cost abstraction

✓ implementing a formatter is really easy with TextFormatterImpl .
✓ no implementation details are leaked is the public API ( TextFormatter ).
✓ it is not possible override the public methods of TextFormatter (unlike the initial version using a trait)

@alice could you please access the readability of this version :wink:

EDIT:
The only that I dislike is that when using a TextFormatter, we must be generic over `TextFormatterImpl:

pub fn usage<TF: TextFormatterImpl>(formatter: &TextFormatter<TF>, some_value: i32) { /* ... */ }

I guess there is no way to write something like

pub fn usage(formatter: &impl TextFormatter, some_value: i32) { /* ... */ }

EDIT2: It's not possible with current Rust. Work is being done, but will wait until the integration of chalk. See my other question.