Why do i need to declare the generic using turbofish if im using the fixed type variant?

#1

i have the funcion

  pub fn make_response<T>(r: &crate::app_logic::Result<T>) -> String
    where
        T: Serialize,
    {
        match r
        {
            Ok(res) => serde_json::to_string(&Response::new(Some(res), &super::ERRORS::OK)).unwrap(),
            Err(e) =>
            {
                serde_json::to_string(&Response::<Option<crate::models::User>>::new(None, e)).unwrap()
            }
        }
    }

which uses Serializible generic T so as you may see here on the error case i have to use turbofish with the Response although its just None. Why is this really necessary?

I am passing there the None variant which is Simply the part of Option itself and has nothing to do with the generic T.

I hope I expressed myself clear enough.

#2

The None is not part of the Option, because Option is not an enum by itself, it’s a class of enums. And each Option<T> has its own None. The most obvious explanation for this (not the real reason, ofc) is the fact that both Option<T> variants must have the same size, so the sizeof(None) will depend on the kind of Option it comes from.

1 Like
#3

That makes a lot of sense. Thank you for the clarification. I was thinking something completely different about the generics.