Why can't I return the generic type T from this function?

bear

I want to return the reference type, but the first make_debug function implementation does not compile. What is the difference between using T or impl std::fmt::Debug for the return type of this function signature?

fn test() -> impl std::fmt::Debug + 'static {
    let value = "v".to_string();
    make_debug(&value)
}

// This function not work
// fn make_debug<'a, T: std::fmt::Debug>(_: &'a T) -> T {
//     42u8
// }

// This one works well
fn make_debug<'a, T: std::fmt::Debug>(_: &'a T) -> impl std::fmt::Debug {
    42u8
}

Playground

Mahmoud Farouq

Using -> impl std::fmt::Debug means "Hey, I'm gonna return you something of a type that implements Debug, but i don't know what that type is".

while using -> T means "Hey, I'm gonna return something of the T type, which happens to implement Debug"

So the difference is, in the first case you can return a different type than the input and it will work as long as it implements the Debug trait. While the second case you MUST return something of the same type as the Input.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I cast `this` to a generic type?

From Dev

Return HashSet<T> from HashSet of generic type in generic function

From Dev

Why can't auto be return type of a function?

From Dev

Why can't I return a reference from this template function?

From Dev

Why can't I return a reference from this template function?

From Dev

Why can't C# compiler infer generic-type delegate from function signature?

From Dev

Why can't I create an array of an inner class of a generic type?

From Dev

Why can't I use covariance with two generic type parameters?

From Dev

Why can't I use a Guid as a generic type constraint?

From Dev

Why can't I cast one instantiation of a generic type to another?

From Dev

Why can't I convert this type into a generic one?

From Dev

How can i return a generic List<T> from a generic method<T>()

From Dev

why i can't get data from js function return from database in codeigniter

From Dev

How to Return a Generic List (Of T) from a Function

From Dev

Why can't we extend a generic type?

From Dev

Why can't we extend a generic type?

From Dev

why can't i return array values to main function?

From Dev

Why can't I use a generic type to implement a non-generic signature

From Dev

Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

From Dev

Why can't I access properties of an anonymous type returned from a function via the dynamic keyword?

From Dev

Why doesn't swift infer the appropriate overload function with a generic return argument without a type constraint?

From Dev

why i can't declare a generic form

From Dev

Why can't I return a unique_ptr from a pair?

From Dev

Why can't I return an &str value generated from a String?

From Java

A lambda's return type can be deduced by the return value, so why can't a function's?

From Dev

Why can't a return type be function type but function pointer in c++?

From Dev

Why can't i use Type variable as a Type parameter to a generic class

From Dev

Why can I assign the return value of a function that returns `const T&`, to a `T` variable but not to a `T&` variable?

From Dev

Why can't a protocol not be used as a type for a generic type in swift?

Related Related

  1. 1

    Why can't I cast `this` to a generic type?

  2. 2

    Return HashSet<T> from HashSet of generic type in generic function

  3. 3

    Why can't auto be return type of a function?

  4. 4

    Why can't I return a reference from this template function?

  5. 5

    Why can't I return a reference from this template function?

  6. 6

    Why can't C# compiler infer generic-type delegate from function signature?

  7. 7

    Why can't I create an array of an inner class of a generic type?

  8. 8

    Why can't I use covariance with two generic type parameters?

  9. 9

    Why can't I use a Guid as a generic type constraint?

  10. 10

    Why can't I cast one instantiation of a generic type to another?

  11. 11

    Why can't I convert this type into a generic one?

  12. 12

    How can i return a generic List<T> from a generic method<T>()

  13. 13

    why i can't get data from js function return from database in codeigniter

  14. 14

    How to Return a Generic List (Of T) from a Function

  15. 15

    Why can't we extend a generic type?

  16. 16

    Why can't we extend a generic type?

  17. 17

    why can't i return array values to main function?

  18. 18

    Why can't I use a generic type to implement a non-generic signature

  19. 19

    Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

  20. 20

    Why can't I access properties of an anonymous type returned from a function via the dynamic keyword?

  21. 21

    Why doesn't swift infer the appropriate overload function with a generic return argument without a type constraint?

  22. 22

    why i can't declare a generic form

  23. 23

    Why can't I return a unique_ptr from a pair?

  24. 24

    Why can't I return an &str value generated from a String?

  25. 25

    A lambda's return type can be deduced by the return value, so why can't a function's?

  26. 26

    Why can't a return type be function type but function pointer in c++?

  27. 27

    Why can't i use Type variable as a Type parameter to a generic class

  28. 28

    Why can I assign the return value of a function that returns `const T&`, to a `T` variable but not to a `T&` variable?

  29. 29

    Why can't a protocol not be used as a type for a generic type in swift?

HotTag

Archive