Code Logo

Access Tuple with .0 .1

Published at25 Jul 2026
Rust Data Structures Easy 0 views
Like0

Write a Rust function that takes a tuple of two integers (i32, i32) and returns the first element using the .0 field access syntax. Rust tuples use dot notation with numeric indices (.0, .1, .2, etc.) to access individual fields, which is unique among mainstream programming languages.

Tuples in Rust are fixed-length collections of heterogeneous values. Unlike arrays where all elements are the same type, tuples can hold different types. The .0 syntax accesses the first element, .1 the second, and so on. This field access is resolved at compile time, so it has zero runtime overhead.

Tuples are commonly used in Rust for returning multiple values from functions, destructuring in match arms, and as lightweight data structures when a named struct would be overkill. The dot-index syntax is intuitive and integrates well with Rust's type inference and pattern matching.

Time complexity is O(1) as tuple field access is a simple memory offset calculation at compile time. Space complexity is O(1) as the tuple elements are stored inline without indirection.

Edge cases include tuples with negative values, tuples with large values, and ensuring the function works with the (i32, i32) type signature. Tuple indexing bounds are checked at compile time, so accessing .0 or .1 is always safe for a 2-element tuple.

Example Input & Output

Example 1
Input
(42,99)
Output
99
Explanation

Second element

Example 2
Input
(5,10)
Output
5
Explanation

First element via .0

Example 3
Input
(-1,7)
Output
-1
Explanation

Negative first element

Example 4
Input
(42,99)
Output
42
Explanation

First element

Example 5
Input
(5,10)
Output
10
Explanation

Second element via .1

Algorithm Flow

Recommendation Algorithm Flow for Access Tuple with .0 .1
Recommendation Algorithm Flow for Access Tuple with .0 .1

Solution Approach

Access an element of a tuple by its index using dot notation. Tuples in Rust are fixed-size collections of values of potentially different types. Elements are accessed using a dot followed by the index: tuple.0, tuple.1, etc. Indices start at 0.

fn solution(t: (i32, f64, String)) -> f64 {
    t.1
}

Tuple indexing is a zero-cost abstraction resolved at compile time. The compiler knows the type of each field, so accessing an element is as fast as accessing a local variable.

Time complexity is O(1), space complexity is O(1).

Best Answers

rust - Approach 1
fn solution(pair: (i32, i32)) -> i32 {
    pair.0
}