Skip to content

impl core::fmt::String for tuples #21189

@jameshfisher

Description

@jameshfisher

I'm just playing with Rust, but I quickly hit a problem with this toy program:

fn main () {
  println!("{}", (1,2));
}

I expected this program to print the string "(1,2)". But compiling it told me:

error: the trait `core::fmt::String` is not implemented for the type `(_, _)`

I found this surprising, but then tried to implement this trait myself by writing:

extern crate core;
use core::fmt;
impl core::fmt::String for (i32,i32) { /* ... */ }
fn main () {
  println!("{}", (1,2));
}

Compiling this gave me the error:

error: the type `(i32, i32)` does not reference any types defined in this crate; only traits defined in the current crate can be implemented for arbitrary types [E0117]

Thanks to some people on the Rust subreddit, I now understand that this is essentially Haskell's "orphan instance" warning, but manifest as an error instead of a warning.

I initially suggested that the problem here was this "orphan instance" policy, but I now think the problem is simply the one that the compiler originally told me: core::fmt::String is not implemented for the type (_, _).

Is there a technical reason that there is no core::fmt::String implementation for tuples? Because, coming from Haskell, I was expecting an equivalent of the Show instance for tuples. I think others, and not just people that have used Haskell, would expect to be able to print tuples without any fuss.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions