Skip to content

Support placeholder syntax for length parameter in array types, when length can be inferred #49999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
peterjoel opened this issue Apr 16, 2018 · 1 comment

Comments

@peterjoel
Copy link
Contributor

peterjoel commented Apr 16, 2018

Consider code like this:

let values: [i32; 3] = [1, 2, 3];
do_stuff(&values);

This becomes annoying when you change the values on the right hand side, because you have to update the length on the left to match. In this case, it's often not necessary to be explicit about the type because it can be fully inferred. But there are cases where it's less convenient, such as with trait objects, where the inference can be too narrow without an annotation:

struct MyStruct;
trait MyTrait {}
impl MyTrait for MyStruct {}

fn main() {
    let values = [&MyStruct, &MyStruct];
    do_stuff(&values);
}

fn do_stuff(values: &[&MyTrait]) {}

This is an error unless you give a hint to the compiler that the array is of the trait object and not the impl. You can do it like this:

let values = [&MyStruct as &MyTrait, &MyStruct];

Which works, but the following would be much clearer:

let values: [&MyTrait; _] =  [&MyStruct, &MyStruct];
@peterjoel peterjoel changed the title Placeholder syntax should be supported for length parameter in array types, where length can be inferred Support placeholder syntax for length parameter in array types, when length can be inferred Apr 16, 2018
@peterjoel
Copy link
Contributor Author

Closing. This actually a duplicate of #40269

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant