Why is it possible to implement a trait for both `T: Display` and `str`?
Clash 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
?
rust
add a comment |Â
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
?
rust
add a comment |Â
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
?
rust
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
rust
edited 2 hours ago
asked 2 hours ago


kazemakase
12.2k22157
12.2k22157
add a comment |Â
add a comment |Â
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
// ^~~~~~~~
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 forT
in the other impl, so they don't overlap. What you are seeing there is the theShow
impl for&str
notstr
.
– Peter Hall
1 hour ago
2
@PeterHall I know! That's why I wrote: looks like.
– Boiethios
1 hour ago
 |Â
show 2 more comments
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
// ^~~~~~~~
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 forT
in the other impl, so they don't overlap. What you are seeing there is the theShow
impl for&str
notstr
.
– Peter Hall
1 hour ago
2
@PeterHall I know! That's why I wrote: looks like.
– Boiethios
1 hour ago
 |Â
show 2 more comments
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
// ^~~~~~~~
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 forT
in the other impl, so they don't overlap. What you are seeing there is the theShow
impl for&str
notstr
.
– Peter Hall
1 hour ago
2
@PeterHall I know! That's why I wrote: looks like.
– Boiethios
1 hour ago
 |Â
show 2 more comments
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
// ^~~~~~~~
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
// ^~~~~~~~
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 forT
in the other impl, so they don't overlap. What you are seeing there is the theShow
impl for&str
notstr
.
– Peter Hall
1 hour ago
2
@PeterHall I know! That's why I wrote: looks like.
– Boiethios
1 hour ago
 |Â
show 2 more comments
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 forT
in the other impl, so they don't overlap. What you are seeing there is the theShow
impl for&str
notstr
.
– 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
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password