Why is it possible to implement a trait for both `T: Display` and `str`?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












There is no specialization in stable Rust yet, so this does not work:



trait X 

impl<T> X for T
impl X for u32 // conflicting implementation


No surprises there: X is implemented for any type T and we can't implement it again for u32.



Surprisingly, the following snippet compiles successfuly:



use std::fmt::Display;

pub trait Show

impl<T: Display> Show for T

impl Show for str

// These impls would cause "conflicting implementation" errors:
// impl<'a> Show for &'a str
// impl Show for String

fn main()


I would not expect this code to compile because Display is implemented for str, so the generic impl should implement Show for str and conflict with the specific impl.



Why does impl Show for str not conflict with impl<T: Display> Show for T?










share|improve this question



























    up vote
    7
    down vote

    favorite












    There is no specialization in stable Rust yet, so this does not work:



    trait X 

    impl<T> X for T
    impl X for u32 // conflicting implementation


    No surprises there: X is implemented for any type T and we can't implement it again for u32.



    Surprisingly, the following snippet compiles successfuly:



    use std::fmt::Display;

    pub trait Show

    impl<T: Display> Show for T

    impl Show for str

    // These impls would cause "conflicting implementation" errors:
    // impl<'a> Show for &'a str
    // impl Show for String

    fn main()


    I would not expect this code to compile because Display is implemented for str, so the generic impl should implement Show for str and conflict with the specific impl.



    Why does impl Show for str not conflict with impl<T: Display> Show for T?










    share|improve this question

























      up vote
      7
      down vote

      favorite









      up vote
      7
      down vote

      favorite











      There is no specialization in stable Rust yet, so this does not work:



      trait X 

      impl<T> X for T
      impl X for u32 // conflicting implementation


      No surprises there: X is implemented for any type T and we can't implement it again for u32.



      Surprisingly, the following snippet compiles successfuly:



      use std::fmt::Display;

      pub trait Show

      impl<T: Display> Show for T

      impl Show for str

      // These impls would cause "conflicting implementation" errors:
      // impl<'a> Show for &'a str
      // impl Show for String

      fn main()


      I would not expect this code to compile because Display is implemented for str, so the generic impl should implement Show for str and conflict with the specific impl.



      Why does impl Show for str not conflict with impl<T: Display> Show for T?










      share|improve this question















      There is no specialization in stable Rust yet, so this does not work:



      trait X 

      impl<T> X for T
      impl X for u32 // conflicting implementation


      No surprises there: X is implemented for any type T and we can't implement it again for u32.



      Surprisingly, the following snippet compiles successfuly:



      use std::fmt::Display;

      pub trait Show

      impl<T: Display> Show for T

      impl Show for str

      // These impls would cause "conflicting implementation" errors:
      // impl<'a> Show for &'a str
      // impl Show for String

      fn main()


      I would not expect this code to compile because Display is implemented for str, so the generic impl should implement Show for str and conflict with the specific impl.



      Why does impl Show for str not conflict with impl<T: Display> Show for T?







      rust






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago

























      asked 2 hours ago









      kazemakase

      12.2k22157




      12.2k22157






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          10
          down vote



          accepted










          The bound <T: Display> implicitly assumes T must be a Sized type. However str is unsized. Therefore the two impls are not in conflict with each other.



          If you need to cover unsized types like str as well, you need to relax the Sized requirement by adding T: ?Sized:



          impl<T: Display + ?Sized> Show for T 
          // ^~~~~~~~





          share|improve this answer






















          • You beat me to it by about 10 seconds! :)
            – Peter Hall
            1 hour ago










          • Yeah, there were too many of us after this one.
            – E_net4
            1 hour ago






          • 1




            @Boiethios that's exactly why I stumbled over this. Actually kinda nice that it allows to "specialize" for string slices.
            – kazemakase
            1 hour ago






          • 1




            @Boiethios It's not specialization. str does not satisfy the requirements for T in the other impl, so they don't overlap. What you are seeing there is the the Show impl for &str not str.
            – Peter Hall
            1 hour ago







          • 2




            @PeterHall I know! That's why I wrote: looks like.
            – Boiethios
            1 hour ago










          Your Answer





          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52552212%2fwhy-is-it-possible-to-implement-a-trait-for-both-t-display-and-str%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          10
          down vote



          accepted










          The bound <T: Display> implicitly assumes T must be a Sized type. However str is unsized. Therefore the two impls are not in conflict with each other.



          If you need to cover unsized types like str as well, you need to relax the Sized requirement by adding T: ?Sized:



          impl<T: Display + ?Sized> Show for T 
          // ^~~~~~~~





          share|improve this answer






















          • You beat me to it by about 10 seconds! :)
            – Peter Hall
            1 hour ago










          • Yeah, there were too many of us after this one.
            – E_net4
            1 hour ago






          • 1




            @Boiethios that's exactly why I stumbled over this. Actually kinda nice that it allows to "specialize" for string slices.
            – kazemakase
            1 hour ago






          • 1




            @Boiethios It's not specialization. str does not satisfy the requirements for T in the other impl, so they don't overlap. What you are seeing there is the the Show impl for &str not str.
            – Peter Hall
            1 hour ago







          • 2




            @PeterHall I know! That's why I wrote: looks like.
            – Boiethios
            1 hour ago














          up vote
          10
          down vote



          accepted










          The bound <T: Display> implicitly assumes T must be a Sized type. However str is unsized. Therefore the two impls are not in conflict with each other.



          If you need to cover unsized types like str as well, you need to relax the Sized requirement by adding T: ?Sized:



          impl<T: Display + ?Sized> Show for T 
          // ^~~~~~~~





          share|improve this answer






















          • You beat me to it by about 10 seconds! :)
            – Peter Hall
            1 hour ago










          • Yeah, there were too many of us after this one.
            – E_net4
            1 hour ago






          • 1




            @Boiethios that's exactly why I stumbled over this. Actually kinda nice that it allows to "specialize" for string slices.
            – kazemakase
            1 hour ago






          • 1




            @Boiethios It's not specialization. str does not satisfy the requirements for T in the other impl, so they don't overlap. What you are seeing there is the the Show impl for &str not str.
            – Peter Hall
            1 hour ago







          • 2




            @PeterHall I know! That's why I wrote: looks like.
            – Boiethios
            1 hour ago












          up vote
          10
          down vote



          accepted







          up vote
          10
          down vote



          accepted






          The bound <T: Display> implicitly assumes T must be a Sized type. However str is unsized. Therefore the two impls are not in conflict with each other.



          If you need to cover unsized types like str as well, you need to relax the Sized requirement by adding T: ?Sized:



          impl<T: Display + ?Sized> Show for T 
          // ^~~~~~~~





          share|improve this answer














          The bound <T: Display> implicitly assumes T must be a Sized type. However str is unsized. Therefore the two impls are not in conflict with each other.



          If you need to cover unsized types like str as well, you need to relax the Sized requirement by adding T: ?Sized:



          impl<T: Display + ?Sized> Show for T 
          // ^~~~~~~~






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 2 hours ago









          kennytm

          393k75888906




          393k75888906











          • You beat me to it by about 10 seconds! :)
            – Peter Hall
            1 hour ago










          • Yeah, there were too many of us after this one.
            – E_net4
            1 hour ago






          • 1




            @Boiethios that's exactly why I stumbled over this. Actually kinda nice that it allows to "specialize" for string slices.
            – kazemakase
            1 hour ago






          • 1




            @Boiethios It's not specialization. str does not satisfy the requirements for T in the other impl, so they don't overlap. What you are seeing there is the the Show impl for &str not str.
            – Peter Hall
            1 hour ago







          • 2




            @PeterHall I know! That's why I wrote: looks like.
            – Boiethios
            1 hour ago
















          • You beat me to it by about 10 seconds! :)
            – Peter Hall
            1 hour ago










          • Yeah, there were too many of us after this one.
            – E_net4
            1 hour ago






          • 1




            @Boiethios that's exactly why I stumbled over this. Actually kinda nice that it allows to "specialize" for string slices.
            – kazemakase
            1 hour ago






          • 1




            @Boiethios It's not specialization. str does not satisfy the requirements for T in the other impl, so they don't overlap. What you are seeing there is the the Show impl for &str not str.
            – Peter Hall
            1 hour ago







          • 2




            @PeterHall I know! That's why I wrote: looks like.
            – Boiethios
            1 hour ago















          You beat me to it by about 10 seconds! :)
          – Peter Hall
          1 hour ago




          You beat me to it by about 10 seconds! :)
          – Peter Hall
          1 hour ago












          Yeah, there were too many of us after this one.
          – E_net4
          1 hour ago




          Yeah, there were too many of us after this one.
          – E_net4
          1 hour ago




          1




          1




          @Boiethios that's exactly why I stumbled over this. Actually kinda nice that it allows to "specialize" for string slices.
          – kazemakase
          1 hour ago




          @Boiethios that's exactly why I stumbled over this. Actually kinda nice that it allows to "specialize" for string slices.
          – kazemakase
          1 hour ago




          1




          1




          @Boiethios It's not specialization. str does not satisfy the requirements for T in the other impl, so they don't overlap. What you are seeing there is the the Show impl for &str not str.
          – Peter Hall
          1 hour ago





          @Boiethios It's not specialization. str does not satisfy the requirements for T in the other impl, so they don't overlap. What you are seeing there is the the Show impl for &str not str.
          – Peter Hall
          1 hour ago





          2




          2




          @PeterHall I know! That's why I wrote: looks like.
          – Boiethios
          1 hour ago




          @PeterHall I know! That's why I wrote: looks like.
          – Boiethios
          1 hour ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52552212%2fwhy-is-it-possible-to-implement-a-trait-for-both-t-display-and-str%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          Confectionery