Why can tuples only handle 12 elements at max?

Why can tuples only handle 12 elements at max?

They can handle more. There might be an upper limit to how many you can fit due to compiler restrictions.

You can put more elements into a tuple, but it won’t implement common traits like PartialEq, Eq,… if all types inside implement them.
It’s because these traits have to be implemented for each tuple “size”, maybe one day it’ll be like arrays (const generics) and this limitation will disappear, for a tuple you need variadic generics. There is a draft RFC but I can’t find a RFC.

A bit of dabbling tells that it is possible to have a 131,072-element tuple before the compiler breaks. Even adding a comma breaks it. Although I still think that even a 15 element tuple is overkill.

2 Likes

Copy and Clone are special,so they are bad examples of traits that tuples over 12 elements don’t implement.

This compiles and runs fine:

fn main(){
    let a=(0,1,2,3,4,5,6,7,8,9,10,11,12);
    let b=a;
    let c=a;
    let d=a.clone();
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6d3d78c350258c79c48cb7d1062e2a47

1 Like

Thank you, I trusted the documentation without testing it. Should the doc be changed?

Maybe,I don’t know how much of a problem it is for the docs to not mention those 2 traits being implemented for all aritys.It’s not very often that people want tuples with more than 12 elements anyway.

Looks like it’s not the compiler that breaks, but the playground simply doesn’t let it to run for too long (and sent a SIGKILL). I’ll try and replicate it locally.

2 Likes

Because language features to allow traits to be implemented for all tuples don’t exist yet, the same way that the features to allow traits to be implemented for all arrays are only just getting implemented, so 33-element arrays don’t implement certain things.

2 Likes

Well, I’ve tried to replicate it locally. RLS tried hard to send me to hell, but simple cargo run worked, even after adding some more elements. Furthermore, I’ve created a tuple of 600000 elements (40000 lines): the build took about a half of minute, but it compiled. And one more thing: I’ve built in in release mode, with a println!("{}", x.599999) added at the end, - and it compiled (in about two minutes and a half), but caused a stack overflow.

I won’t try and do this at larger scales - I think the picture is clear.

2 Likes