How to organize chain processing in Rust?

⚓ Rust    📅 2025-12-10    👤 surdeus    👁️ 8      

surdeus

Warning

This post was published 38 days ago. The information described in this article may have changed.

I created a trait like:

pub trait Colorized : std::fmt::Display {
    fn green(&self) -> ??? {
    }
    fn bright(&self) -> ??? {
    }
....

So, I want to organize a chain of calls to the trait, for example:

println!("Colorized {}", message.green().bright());

Certainly I can implement the trait for str, but if I return str, then I will lose color attributes I set before. So an idea is to return ColorAttributeHolder. More likely, I will need to implement Colorized for it too, to be able to chain my calls. However I have few questions:

  1. where and how store the original caller of Colorized?
  2. how to pass Colorized to the next chained calls? The best is by a reference, but the first self and the next self can be different and again I will lose valuable information.

Probably, my approach is wrong, so please share your approach.

3 posts - 3 participants

Read full topic

🏷️ Rust_feed