Error that seems to contradict itself (and I can't see the reason for the error in any case)


I get the following error:

error[E0308]: mismatched types
   --> owo-colors/src/
193 |     pub fn apply_to<'a, T>(&self, target: &'a T) -> Styled<&'a T> {
    |                         - this type parameter
194 |         Styled::new(target, *self)
    |                     ^^^^^^ expected `&T`, found type parameter `T`
    = note: expected reference `&&T`
               found reference `&'a T`

And I really don't get it.

First, it says 'expected &T, found type parameter T' and then it says 'note: expected reference &&T found reference &'a T'.

I don't see how there could be any T, or what would expect &&T.

I spend already more than an hour to find the reason for this error, so any help is greatly appreciated! :sweat_smile:

Here is the relevant code:

// Btw., it's correct to derive `Copy` here because it only contains
// `&static str` and `bool` values, or would it still make sense to
// work with references?
#[derive(Default, Copy, Clone)]
pub struct Style {
    fg: Option<&'static str>,
    bg: Option<&'static str>,
    bold: bool,
    dimmed: bool,
    italic: bool,
    underline: bool,
    blink: bool,
    blink_fast: bool,
    reversed: bool,
    hidden: bool,
    strikethrough: bool,

impl Style {
    pub fn new() -> Self {

    pub fn apply_to<'a, T>(&self, target: &'a T) -> Styled<&'a T> {
        Styled::new(target, *self)
        // ^ This where the error happens

    /* more code */


pub struct Styled<'a, T> {
    target: &'a T,
    style: Style,

impl<'a, T> Styled<'a, T> {
    fn new(target: &'a T, style: Style) -> Self {
        Self {target, style}

Let me know if the problem could be somewhere else, and I will publish the code somewhere.

If a Styled<T1> contains a &T1, then a Styled<&T2> contains a &&T2. Use the type Styled<'a, T> instead.


@alice : Wow, thank you! I probably never would have found the solution on my own... :sweat_smile:

I'd like to read more about why this happens, but I can't even come up with search terms for Google. If anybody can give me a hint, that would be great.

I mean, it just puts the type you told it right where you told it to put it. Maybe substitution is a search term?

1 Like

Ah, now, while writing why this behavior makes no sense to me, I got it! :laughing: