rust-lang / rust-lang/rfcs

Add method or trait impl for converting reference of tuple to tuple of references

Open
#3,520 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Not sure what's the "formal" syntax to express "for tuples of any size", so I'll use 2-tuples here in my examples.

TL;DR, I want this:

let ref_of_tuple: &(u8, u16) = &(6, 9);
let tuple_of_refs: (&u8, &u16) = ref_of_tuple.as_ref();
assert_eq!(tuple_of_refs, (&6, &9));

Much like how Option::as_ref converts from &Option<T> to Option<&T>.


Besides being generally useful, it also allows for more readable code in some circumstances:

fn convert(from: &[(u8, u16)]) -> Vec<(&u8, &u16)> {
    from.iter()
        .map(|(a, b)| (a, b)) // huh?
        .collect()
}

It looks like the map isn't doing anything, but due to how destructuring works, it's actually necessary. This would be much clearer:

fn convert(from: &[(u8, u16)]) -> Vec<(&u8, &u16)> {
    from.iter()
        .map(AsRef::as_ref)
        .collect()
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Rust examples in the issue and compare the proposed AsRef::as_ref usage with the existing Option::as_ref behavior. Resolve the API and scope for supporting tuples of arbitrary size; done means the design question has an agreed direction and implementation/testing plan.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.