What is the type for a mutable reference of impl trait argument

Assuming I have a trait called Writer

pub trait Writer{
  fn write(&mut self, content: AsRef<str>);
}

Now I have a function that want to take the mutable reference of the writer, e.g.

/// Note: the below writer type doesn't compile
fn write_template(template: &str, writer: &mut impl Writer) {
   ...
} 

I am struggling to figure out the type for writer in the write_template function. Is there a good way to do that? Do I have to use dyn keyword?

Thanks in advance!

AsRef is a trait, you must add impl

pub trait Writer{
  fn write(&mut self, content: impl AsRef<str>);
}

fn write_template(template: &str, writer: &mut impl Writer) {

}

Thanks... I realize I made a silly mistake. Thank for the help!

Note that you should generally prefer directly using &str instead of impl AsRef<str>, see this thread for more info

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.