Skip to main content

Triple

Struct Triple 

Source
pub struct Triple<F, S, T> {
    pub first: F,
    pub second: S,
    pub third: T,
}
Expand description

A generic triple structure that holds three values.

§Type Parameters

  • F - The type of the first element
  • S - The type of the second element
  • T - The type of the third element

§Comparison with Native Tuples

While Rust’s native tuples (F, S, T) and Triple<F, S, T> both store three values, they differ in several key aspects:

§Similarities

  • Both can hold three values of different types
  • Both support pattern matching and destructuring
  • Both can be converted to each other via From/Into traits
  • Both have similar memory layout and zero runtime overhead

§Differences

FeatureNative Tuple (F, S, T)Triple<F, S, T>
Field Access.0, .1, .2 (positional).first, .second, .third (named)
SemanticsAnonymous, positionalSelf-documenting, semantic
MethodsLimited built-in methodsRich API (map, getters, setters)
ReadabilityLess clear in complex codeMore expressive and maintainable
Type AliasHard to create meaningful aliasesEasy to create domain-specific types

§When to Use Triple vs Native Tuples

Use Triple when:

  • Field names improve code readability (e.g., triple.first vs tuple.0)
  • You need additional methods like map_first(), map_second(), etc.
  • Creating domain-specific types (e.g., type Coordinate = Triple<f64, f64, f64>)
  • The triple represents a logical entity with semantic meaning
  • You want to implement custom traits or behaviors

Use native tuples when:

  • You need quick, temporary grouping of values
  • Working with existing APIs that expect tuples
  • Pattern matching is the primary use case
  • Minimal overhead and simplicity are paramount

§Examples

use qubit_common::Triple;

let triple = Triple::new("name", 42, true);
assert_eq!(triple.first, "name");
assert_eq!(triple.second, 42);
assert_eq!(triple.third, true);

let triple = Triple { first: 1, second: 2.5, third: "hello" };
assert_eq!(triple.first, 1);
assert_eq!(triple.second, 2.5);
assert_eq!(triple.third, "hello");

Fields§

§first: F

The first element of the triple

§second: S

The second element of the triple

§third: T

The third element of the triple

Implementations§

Source§

impl<F, S, T> Triple<F, S, T>

Source

pub fn new(first: F, second: S, third: T) -> Self

Creates a new Triple with the given values.

§Arguments
  • first - The first element
  • second - The second element
  • third - The third element
§Examples
use qubit_common::Triple;

let triple = Triple::new(1, "hello", true);
assert_eq!(triple.first, 1);
assert_eq!(triple.second, "hello");
assert_eq!(triple.third, true);
Examples found in repository?
examples/util_demo.rs (line 62)
22fn main() {
23    println!("=== Pair Demo ===\n");
24
25    // Create Pair using new method
26    let pair1 = Pair::new("name", "Alice");
27    println!("pair1 (using new): {}", pair1);
28
29    // Create Pair using struct literal (public fields)
30    let mut pair2 = Pair {
31        first: 1,
32        second: 100,
33    };
34    println!("pair2 (using struct literal): {}", pair2);
35
36    // Direct field access (public fields)
37    pair2.first = 42;
38    pair2.second = 200;
39    println!("pair2 (after modification): {}", pair2);
40
41    // Using methods
42    let pair3 = Pair::new(10, 20);
43    println!("pair3: {}", pair3);
44    println!("  first: {}, second: {}", pair3.first(), pair3.second());
45
46    // Map operations
47    let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
48    println!("pair4 (after mapping): {}", pair4);
49
50    // Swap
51    let pair5 = Pair::new("key", 123);
52    let swapped = pair5.swap();
53    println!("pair5: {}, swapped: {}", pair5, swapped);
54
55    // Convert to tuple
56    let (f, s) = pair5.into_tuple();
57    println!("Converted to tuple: ({}, {})", f, s);
58
59    println!("\n=== Triple Demo ===\n");
60
61    // Create Triple using new method
62    let triple1 = Triple::new("Alice", 30, true);
63    println!("triple1 (using new): {}", triple1);
64
65    // Create Triple using struct literal (public fields)
66    let mut triple2 = Triple {
67        first: 1,
68        second: 2,
69        third: 3,
70    };
71    println!("triple2 (using struct literal): {}", triple2);
72
73    // Direct field access (public fields)
74    triple2.first = 10;
75    triple2.second = 20;
76    triple2.third = 30;
77    println!("triple2 (after modification): {}", triple2);
78
79    // Using methods
80    let triple3 = Triple::new(100, 200, 300);
81    println!("triple3: {}", triple3);
82    println!(
83        "  first: {}, second: {}, third: {}",
84        triple3.first(),
85        triple3.second(),
86        triple3.third()
87    );
88
89    // Map operations
90    let triple4 = triple3
91        .map_first(|x| x / 10)
92        .map_second(|x| x / 10)
93        .map_third(|x| x / 10);
94    println!("triple4 (after mapping): {}", triple4);
95
96    // Convert to tuple
97    let (f, s, t) = triple4.into_tuple();
98    println!("Converted to tuple: ({}, {}, {})", f, s, t);
99
100    println!("\n=== Advanced Usage ===\n");
101
102    // Pair with different types
103    let config = Pair {
104        first: "database_url",
105        second: "postgresql://localhost/mydb",
106    };
107    println!("Config: {}", config);
108
109    // Triple as a record
110    let record = Triple {
111        first: "user_001",
112        second: "Alice",
113        third: 25,
114    };
115    println!(
116        "User Record: ID={}, Name={}, Age={}",
117        record.first, record.second, record.third
118    );
119
120    // Nested structures
121    let nested = Pair {
122        first: Pair::new(1, 2),
123        second: Triple::new(3, 4, 5),
124    };
125    println!(
126        "Nested: pair=({}, {}), triple=({}, {}, {})",
127        nested.first.first,
128        nested.first.second,
129        nested.second.first,
130        nested.second.second,
131        nested.second.third
132    );
133}
Source

pub fn into_tuple(self) -> (F, S, T)

Consumes the triple and returns a tuple of its elements.

§Examples
use qubit_common::Triple;

let triple = Triple::new(1, "hello", true);
let (first, second, third) = triple.into_tuple();
assert_eq!(first, 1);
assert_eq!(second, "hello");
assert_eq!(third, true);
Examples found in repository?
examples/util_demo.rs (line 97)
22fn main() {
23    println!("=== Pair Demo ===\n");
24
25    // Create Pair using new method
26    let pair1 = Pair::new("name", "Alice");
27    println!("pair1 (using new): {}", pair1);
28
29    // Create Pair using struct literal (public fields)
30    let mut pair2 = Pair {
31        first: 1,
32        second: 100,
33    };
34    println!("pair2 (using struct literal): {}", pair2);
35
36    // Direct field access (public fields)
37    pair2.first = 42;
38    pair2.second = 200;
39    println!("pair2 (after modification): {}", pair2);
40
41    // Using methods
42    let pair3 = Pair::new(10, 20);
43    println!("pair3: {}", pair3);
44    println!("  first: {}, second: {}", pair3.first(), pair3.second());
45
46    // Map operations
47    let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
48    println!("pair4 (after mapping): {}", pair4);
49
50    // Swap
51    let pair5 = Pair::new("key", 123);
52    let swapped = pair5.swap();
53    println!("pair5: {}, swapped: {}", pair5, swapped);
54
55    // Convert to tuple
56    let (f, s) = pair5.into_tuple();
57    println!("Converted to tuple: ({}, {})", f, s);
58
59    println!("\n=== Triple Demo ===\n");
60
61    // Create Triple using new method
62    let triple1 = Triple::new("Alice", 30, true);
63    println!("triple1 (using new): {}", triple1);
64
65    // Create Triple using struct literal (public fields)
66    let mut triple2 = Triple {
67        first: 1,
68        second: 2,
69        third: 3,
70    };
71    println!("triple2 (using struct literal): {}", triple2);
72
73    // Direct field access (public fields)
74    triple2.first = 10;
75    triple2.second = 20;
76    triple2.third = 30;
77    println!("triple2 (after modification): {}", triple2);
78
79    // Using methods
80    let triple3 = Triple::new(100, 200, 300);
81    println!("triple3: {}", triple3);
82    println!(
83        "  first: {}, second: {}, third: {}",
84        triple3.first(),
85        triple3.second(),
86        triple3.third()
87    );
88
89    // Map operations
90    let triple4 = triple3
91        .map_first(|x| x / 10)
92        .map_second(|x| x / 10)
93        .map_third(|x| x / 10);
94    println!("triple4 (after mapping): {}", triple4);
95
96    // Convert to tuple
97    let (f, s, t) = triple4.into_tuple();
98    println!("Converted to tuple: ({}, {}, {})", f, s, t);
99
100    println!("\n=== Advanced Usage ===\n");
101
102    // Pair with different types
103    let config = Pair {
104        first: "database_url",
105        second: "postgresql://localhost/mydb",
106    };
107    println!("Config: {}", config);
108
109    // Triple as a record
110    let record = Triple {
111        first: "user_001",
112        second: "Alice",
113        third: 25,
114    };
115    println!(
116        "User Record: ID={}, Name={}, Age={}",
117        record.first, record.second, record.third
118    );
119
120    // Nested structures
121    let nested = Pair {
122        first: Pair::new(1, 2),
123        second: Triple::new(3, 4, 5),
124    };
125    println!(
126        "Nested: pair=({}, {}), triple=({}, {}, {})",
127        nested.first.first,
128        nested.first.second,
129        nested.second.first,
130        nested.second.second,
131        nested.second.third
132    );
133}
Source

pub fn first(&self) -> &F

Returns a reference to the first element.

§Examples
use qubit_common::Triple;

let triple = Triple::new(1, 2, 3);
assert_eq!(triple.first(), &1);
Examples found in repository?
examples/util_demo.rs (line 84)
22fn main() {
23    println!("=== Pair Demo ===\n");
24
25    // Create Pair using new method
26    let pair1 = Pair::new("name", "Alice");
27    println!("pair1 (using new): {}", pair1);
28
29    // Create Pair using struct literal (public fields)
30    let mut pair2 = Pair {
31        first: 1,
32        second: 100,
33    };
34    println!("pair2 (using struct literal): {}", pair2);
35
36    // Direct field access (public fields)
37    pair2.first = 42;
38    pair2.second = 200;
39    println!("pair2 (after modification): {}", pair2);
40
41    // Using methods
42    let pair3 = Pair::new(10, 20);
43    println!("pair3: {}", pair3);
44    println!("  first: {}, second: {}", pair3.first(), pair3.second());
45
46    // Map operations
47    let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
48    println!("pair4 (after mapping): {}", pair4);
49
50    // Swap
51    let pair5 = Pair::new("key", 123);
52    let swapped = pair5.swap();
53    println!("pair5: {}, swapped: {}", pair5, swapped);
54
55    // Convert to tuple
56    let (f, s) = pair5.into_tuple();
57    println!("Converted to tuple: ({}, {})", f, s);
58
59    println!("\n=== Triple Demo ===\n");
60
61    // Create Triple using new method
62    let triple1 = Triple::new("Alice", 30, true);
63    println!("triple1 (using new): {}", triple1);
64
65    // Create Triple using struct literal (public fields)
66    let mut triple2 = Triple {
67        first: 1,
68        second: 2,
69        third: 3,
70    };
71    println!("triple2 (using struct literal): {}", triple2);
72
73    // Direct field access (public fields)
74    triple2.first = 10;
75    triple2.second = 20;
76    triple2.third = 30;
77    println!("triple2 (after modification): {}", triple2);
78
79    // Using methods
80    let triple3 = Triple::new(100, 200, 300);
81    println!("triple3: {}", triple3);
82    println!(
83        "  first: {}, second: {}, third: {}",
84        triple3.first(),
85        triple3.second(),
86        triple3.third()
87    );
88
89    // Map operations
90    let triple4 = triple3
91        .map_first(|x| x / 10)
92        .map_second(|x| x / 10)
93        .map_third(|x| x / 10);
94    println!("triple4 (after mapping): {}", triple4);
95
96    // Convert to tuple
97    let (f, s, t) = triple4.into_tuple();
98    println!("Converted to tuple: ({}, {}, {})", f, s, t);
99
100    println!("\n=== Advanced Usage ===\n");
101
102    // Pair with different types
103    let config = Pair {
104        first: "database_url",
105        second: "postgresql://localhost/mydb",
106    };
107    println!("Config: {}", config);
108
109    // Triple as a record
110    let record = Triple {
111        first: "user_001",
112        second: "Alice",
113        third: 25,
114    };
115    println!(
116        "User Record: ID={}, Name={}, Age={}",
117        record.first, record.second, record.third
118    );
119
120    // Nested structures
121    let nested = Pair {
122        first: Pair::new(1, 2),
123        second: Triple::new(3, 4, 5),
124    };
125    println!(
126        "Nested: pair=({}, {}), triple=({}, {}, {})",
127        nested.first.first,
128        nested.first.second,
129        nested.second.first,
130        nested.second.second,
131        nested.second.third
132    );
133}
Source

pub fn second(&self) -> &S

Returns a reference to the second element.

§Examples
use qubit_common::Triple;

let triple = Triple::new(1, 2, 3);
assert_eq!(triple.second(), &2);
Examples found in repository?
examples/util_demo.rs (line 85)
22fn main() {
23    println!("=== Pair Demo ===\n");
24
25    // Create Pair using new method
26    let pair1 = Pair::new("name", "Alice");
27    println!("pair1 (using new): {}", pair1);
28
29    // Create Pair using struct literal (public fields)
30    let mut pair2 = Pair {
31        first: 1,
32        second: 100,
33    };
34    println!("pair2 (using struct literal): {}", pair2);
35
36    // Direct field access (public fields)
37    pair2.first = 42;
38    pair2.second = 200;
39    println!("pair2 (after modification): {}", pair2);
40
41    // Using methods
42    let pair3 = Pair::new(10, 20);
43    println!("pair3: {}", pair3);
44    println!("  first: {}, second: {}", pair3.first(), pair3.second());
45
46    // Map operations
47    let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
48    println!("pair4 (after mapping): {}", pair4);
49
50    // Swap
51    let pair5 = Pair::new("key", 123);
52    let swapped = pair5.swap();
53    println!("pair5: {}, swapped: {}", pair5, swapped);
54
55    // Convert to tuple
56    let (f, s) = pair5.into_tuple();
57    println!("Converted to tuple: ({}, {})", f, s);
58
59    println!("\n=== Triple Demo ===\n");
60
61    // Create Triple using new method
62    let triple1 = Triple::new("Alice", 30, true);
63    println!("triple1 (using new): {}", triple1);
64
65    // Create Triple using struct literal (public fields)
66    let mut triple2 = Triple {
67        first: 1,
68        second: 2,
69        third: 3,
70    };
71    println!("triple2 (using struct literal): {}", triple2);
72
73    // Direct field access (public fields)
74    triple2.first = 10;
75    triple2.second = 20;
76    triple2.third = 30;
77    println!("triple2 (after modification): {}", triple2);
78
79    // Using methods
80    let triple3 = Triple::new(100, 200, 300);
81    println!("triple3: {}", triple3);
82    println!(
83        "  first: {}, second: {}, third: {}",
84        triple3.first(),
85        triple3.second(),
86        triple3.third()
87    );
88
89    // Map operations
90    let triple4 = triple3
91        .map_first(|x| x / 10)
92        .map_second(|x| x / 10)
93        .map_third(|x| x / 10);
94    println!("triple4 (after mapping): {}", triple4);
95
96    // Convert to tuple
97    let (f, s, t) = triple4.into_tuple();
98    println!("Converted to tuple: ({}, {}, {})", f, s, t);
99
100    println!("\n=== Advanced Usage ===\n");
101
102    // Pair with different types
103    let config = Pair {
104        first: "database_url",
105        second: "postgresql://localhost/mydb",
106    };
107    println!("Config: {}", config);
108
109    // Triple as a record
110    let record = Triple {
111        first: "user_001",
112        second: "Alice",
113        third: 25,
114    };
115    println!(
116        "User Record: ID={}, Name={}, Age={}",
117        record.first, record.second, record.third
118    );
119
120    // Nested structures
121    let nested = Pair {
122        first: Pair::new(1, 2),
123        second: Triple::new(3, 4, 5),
124    };
125    println!(
126        "Nested: pair=({}, {}), triple=({}, {}, {})",
127        nested.first.first,
128        nested.first.second,
129        nested.second.first,
130        nested.second.second,
131        nested.second.third
132    );
133}
Source

pub fn third(&self) -> &T

Returns a reference to the third element.

§Examples
use qubit_common::Triple;

let triple = Triple::new(1, 2, 3);
assert_eq!(triple.third(), &3);
Examples found in repository?
examples/util_demo.rs (line 86)
22fn main() {
23    println!("=== Pair Demo ===\n");
24
25    // Create Pair using new method
26    let pair1 = Pair::new("name", "Alice");
27    println!("pair1 (using new): {}", pair1);
28
29    // Create Pair using struct literal (public fields)
30    let mut pair2 = Pair {
31        first: 1,
32        second: 100,
33    };
34    println!("pair2 (using struct literal): {}", pair2);
35
36    // Direct field access (public fields)
37    pair2.first = 42;
38    pair2.second = 200;
39    println!("pair2 (after modification): {}", pair2);
40
41    // Using methods
42    let pair3 = Pair::new(10, 20);
43    println!("pair3: {}", pair3);
44    println!("  first: {}, second: {}", pair3.first(), pair3.second());
45
46    // Map operations
47    let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
48    println!("pair4 (after mapping): {}", pair4);
49
50    // Swap
51    let pair5 = Pair::new("key", 123);
52    let swapped = pair5.swap();
53    println!("pair5: {}, swapped: {}", pair5, swapped);
54
55    // Convert to tuple
56    let (f, s) = pair5.into_tuple();
57    println!("Converted to tuple: ({}, {})", f, s);
58
59    println!("\n=== Triple Demo ===\n");
60
61    // Create Triple using new method
62    let triple1 = Triple::new("Alice", 30, true);
63    println!("triple1 (using new): {}", triple1);
64
65    // Create Triple using struct literal (public fields)
66    let mut triple2 = Triple {
67        first: 1,
68        second: 2,
69        third: 3,
70    };
71    println!("triple2 (using struct literal): {}", triple2);
72
73    // Direct field access (public fields)
74    triple2.first = 10;
75    triple2.second = 20;
76    triple2.third = 30;
77    println!("triple2 (after modification): {}", triple2);
78
79    // Using methods
80    let triple3 = Triple::new(100, 200, 300);
81    println!("triple3: {}", triple3);
82    println!(
83        "  first: {}, second: {}, third: {}",
84        triple3.first(),
85        triple3.second(),
86        triple3.third()
87    );
88
89    // Map operations
90    let triple4 = triple3
91        .map_first(|x| x / 10)
92        .map_second(|x| x / 10)
93        .map_third(|x| x / 10);
94    println!("triple4 (after mapping): {}", triple4);
95
96    // Convert to tuple
97    let (f, s, t) = triple4.into_tuple();
98    println!("Converted to tuple: ({}, {}, {})", f, s, t);
99
100    println!("\n=== Advanced Usage ===\n");
101
102    // Pair with different types
103    let config = Pair {
104        first: "database_url",
105        second: "postgresql://localhost/mydb",
106    };
107    println!("Config: {}", config);
108
109    // Triple as a record
110    let record = Triple {
111        first: "user_001",
112        second: "Alice",
113        third: 25,
114    };
115    println!(
116        "User Record: ID={}, Name={}, Age={}",
117        record.first, record.second, record.third
118    );
119
120    // Nested structures
121    let nested = Pair {
122        first: Pair::new(1, 2),
123        second: Triple::new(3, 4, 5),
124    };
125    println!(
126        "Nested: pair=({}, {}), triple=({}, {}, {})",
127        nested.first.first,
128        nested.first.second,
129        nested.second.first,
130        nested.second.second,
131        nested.second.third
132    );
133}
Source

pub fn first_mut(&mut self) -> &mut F

Returns a mutable reference to the first element.

§Examples
use qubit_common::Triple;

let mut triple = Triple::new(1, 2, 3);
*triple.first_mut() = 10;
assert_eq!(triple.first, 10);
Source

pub fn second_mut(&mut self) -> &mut S

Returns a mutable reference to the second element.

§Examples
use qubit_common::Triple;

let mut triple = Triple::new(1, 2, 3);
*triple.second_mut() = 20;
assert_eq!(triple.second, 20);
Source

pub fn third_mut(&mut self) -> &mut T

Returns a mutable reference to the third element.

§Examples
use qubit_common::Triple;

let mut triple = Triple::new(1, 2, 3);
*triple.third_mut() = 30;
assert_eq!(triple.third, 30);
Source

pub fn map_first<F2, Fn>(self, f: Fn) -> Triple<F2, S, T>
where Fn: FnOnce(F) -> F2,

Maps the first element to a new value using the provided function.

§Examples
use qubit_common::Triple;

let triple = Triple::new(1, "hello", true);
let mapped = triple.map_first(|x| x * 2);
assert_eq!(mapped.first, 2);
assert_eq!(mapped.second, "hello");
assert_eq!(mapped.third, true);
Examples found in repository?
examples/util_demo.rs (line 91)
22fn main() {
23    println!("=== Pair Demo ===\n");
24
25    // Create Pair using new method
26    let pair1 = Pair::new("name", "Alice");
27    println!("pair1 (using new): {}", pair1);
28
29    // Create Pair using struct literal (public fields)
30    let mut pair2 = Pair {
31        first: 1,
32        second: 100,
33    };
34    println!("pair2 (using struct literal): {}", pair2);
35
36    // Direct field access (public fields)
37    pair2.first = 42;
38    pair2.second = 200;
39    println!("pair2 (after modification): {}", pair2);
40
41    // Using methods
42    let pair3 = Pair::new(10, 20);
43    println!("pair3: {}", pair3);
44    println!("  first: {}, second: {}", pair3.first(), pair3.second());
45
46    // Map operations
47    let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
48    println!("pair4 (after mapping): {}", pair4);
49
50    // Swap
51    let pair5 = Pair::new("key", 123);
52    let swapped = pair5.swap();
53    println!("pair5: {}, swapped: {}", pair5, swapped);
54
55    // Convert to tuple
56    let (f, s) = pair5.into_tuple();
57    println!("Converted to tuple: ({}, {})", f, s);
58
59    println!("\n=== Triple Demo ===\n");
60
61    // Create Triple using new method
62    let triple1 = Triple::new("Alice", 30, true);
63    println!("triple1 (using new): {}", triple1);
64
65    // Create Triple using struct literal (public fields)
66    let mut triple2 = Triple {
67        first: 1,
68        second: 2,
69        third: 3,
70    };
71    println!("triple2 (using struct literal): {}", triple2);
72
73    // Direct field access (public fields)
74    triple2.first = 10;
75    triple2.second = 20;
76    triple2.third = 30;
77    println!("triple2 (after modification): {}", triple2);
78
79    // Using methods
80    let triple3 = Triple::new(100, 200, 300);
81    println!("triple3: {}", triple3);
82    println!(
83        "  first: {}, second: {}, third: {}",
84        triple3.first(),
85        triple3.second(),
86        triple3.third()
87    );
88
89    // Map operations
90    let triple4 = triple3
91        .map_first(|x| x / 10)
92        .map_second(|x| x / 10)
93        .map_third(|x| x / 10);
94    println!("triple4 (after mapping): {}", triple4);
95
96    // Convert to tuple
97    let (f, s, t) = triple4.into_tuple();
98    println!("Converted to tuple: ({}, {}, {})", f, s, t);
99
100    println!("\n=== Advanced Usage ===\n");
101
102    // Pair with different types
103    let config = Pair {
104        first: "database_url",
105        second: "postgresql://localhost/mydb",
106    };
107    println!("Config: {}", config);
108
109    // Triple as a record
110    let record = Triple {
111        first: "user_001",
112        second: "Alice",
113        third: 25,
114    };
115    println!(
116        "User Record: ID={}, Name={}, Age={}",
117        record.first, record.second, record.third
118    );
119
120    // Nested structures
121    let nested = Pair {
122        first: Pair::new(1, 2),
123        second: Triple::new(3, 4, 5),
124    };
125    println!(
126        "Nested: pair=({}, {}), triple=({}, {}, {})",
127        nested.first.first,
128        nested.first.second,
129        nested.second.first,
130        nested.second.second,
131        nested.second.third
132    );
133}
Source

pub fn map_second<S2, Fn>(self, f: Fn) -> Triple<F, S2, T>
where Fn: FnOnce(S) -> S2,

Maps the second element to a new value using the provided function.

§Examples
use qubit_common::Triple;

let triple = Triple::new(1, "hello", true);
let mapped = triple.map_second(|s| s.len());
assert_eq!(mapped.first, 1);
assert_eq!(mapped.second, 5);
assert_eq!(mapped.third, true);
Examples found in repository?
examples/util_demo.rs (line 92)
22fn main() {
23    println!("=== Pair Demo ===\n");
24
25    // Create Pair using new method
26    let pair1 = Pair::new("name", "Alice");
27    println!("pair1 (using new): {}", pair1);
28
29    // Create Pair using struct literal (public fields)
30    let mut pair2 = Pair {
31        first: 1,
32        second: 100,
33    };
34    println!("pair2 (using struct literal): {}", pair2);
35
36    // Direct field access (public fields)
37    pair2.first = 42;
38    pair2.second = 200;
39    println!("pair2 (after modification): {}", pair2);
40
41    // Using methods
42    let pair3 = Pair::new(10, 20);
43    println!("pair3: {}", pair3);
44    println!("  first: {}, second: {}", pair3.first(), pair3.second());
45
46    // Map operations
47    let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
48    println!("pair4 (after mapping): {}", pair4);
49
50    // Swap
51    let pair5 = Pair::new("key", 123);
52    let swapped = pair5.swap();
53    println!("pair5: {}, swapped: {}", pair5, swapped);
54
55    // Convert to tuple
56    let (f, s) = pair5.into_tuple();
57    println!("Converted to tuple: ({}, {})", f, s);
58
59    println!("\n=== Triple Demo ===\n");
60
61    // Create Triple using new method
62    let triple1 = Triple::new("Alice", 30, true);
63    println!("triple1 (using new): {}", triple1);
64
65    // Create Triple using struct literal (public fields)
66    let mut triple2 = Triple {
67        first: 1,
68        second: 2,
69        third: 3,
70    };
71    println!("triple2 (using struct literal): {}", triple2);
72
73    // Direct field access (public fields)
74    triple2.first = 10;
75    triple2.second = 20;
76    triple2.third = 30;
77    println!("triple2 (after modification): {}", triple2);
78
79    // Using methods
80    let triple3 = Triple::new(100, 200, 300);
81    println!("triple3: {}", triple3);
82    println!(
83        "  first: {}, second: {}, third: {}",
84        triple3.first(),
85        triple3.second(),
86        triple3.third()
87    );
88
89    // Map operations
90    let triple4 = triple3
91        .map_first(|x| x / 10)
92        .map_second(|x| x / 10)
93        .map_third(|x| x / 10);
94    println!("triple4 (after mapping): {}", triple4);
95
96    // Convert to tuple
97    let (f, s, t) = triple4.into_tuple();
98    println!("Converted to tuple: ({}, {}, {})", f, s, t);
99
100    println!("\n=== Advanced Usage ===\n");
101
102    // Pair with different types
103    let config = Pair {
104        first: "database_url",
105        second: "postgresql://localhost/mydb",
106    };
107    println!("Config: {}", config);
108
109    // Triple as a record
110    let record = Triple {
111        first: "user_001",
112        second: "Alice",
113        third: 25,
114    };
115    println!(
116        "User Record: ID={}, Name={}, Age={}",
117        record.first, record.second, record.third
118    );
119
120    // Nested structures
121    let nested = Pair {
122        first: Pair::new(1, 2),
123        second: Triple::new(3, 4, 5),
124    };
125    println!(
126        "Nested: pair=({}, {}), triple=({}, {}, {})",
127        nested.first.first,
128        nested.first.second,
129        nested.second.first,
130        nested.second.second,
131        nested.second.third
132    );
133}
Source

pub fn map_third<T2, Fn>(self, f: Fn) -> Triple<F, S, T2>
where Fn: FnOnce(T) -> T2,

Maps the third element to a new value using the provided function.

§Examples
use qubit_common::Triple;

let triple = Triple::new(1, "hello", true);
let mapped = triple.map_third(|b| if b { "yes" } else { "no" });
assert_eq!(mapped.first, 1);
assert_eq!(mapped.second, "hello");
assert_eq!(mapped.third, "yes");
Examples found in repository?
examples/util_demo.rs (line 93)
22fn main() {
23    println!("=== Pair Demo ===\n");
24
25    // Create Pair using new method
26    let pair1 = Pair::new("name", "Alice");
27    println!("pair1 (using new): {}", pair1);
28
29    // Create Pair using struct literal (public fields)
30    let mut pair2 = Pair {
31        first: 1,
32        second: 100,
33    };
34    println!("pair2 (using struct literal): {}", pair2);
35
36    // Direct field access (public fields)
37    pair2.first = 42;
38    pair2.second = 200;
39    println!("pair2 (after modification): {}", pair2);
40
41    // Using methods
42    let pair3 = Pair::new(10, 20);
43    println!("pair3: {}", pair3);
44    println!("  first: {}, second: {}", pair3.first(), pair3.second());
45
46    // Map operations
47    let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
48    println!("pair4 (after mapping): {}", pair4);
49
50    // Swap
51    let pair5 = Pair::new("key", 123);
52    let swapped = pair5.swap();
53    println!("pair5: {}, swapped: {}", pair5, swapped);
54
55    // Convert to tuple
56    let (f, s) = pair5.into_tuple();
57    println!("Converted to tuple: ({}, {})", f, s);
58
59    println!("\n=== Triple Demo ===\n");
60
61    // Create Triple using new method
62    let triple1 = Triple::new("Alice", 30, true);
63    println!("triple1 (using new): {}", triple1);
64
65    // Create Triple using struct literal (public fields)
66    let mut triple2 = Triple {
67        first: 1,
68        second: 2,
69        third: 3,
70    };
71    println!("triple2 (using struct literal): {}", triple2);
72
73    // Direct field access (public fields)
74    triple2.first = 10;
75    triple2.second = 20;
76    triple2.third = 30;
77    println!("triple2 (after modification): {}", triple2);
78
79    // Using methods
80    let triple3 = Triple::new(100, 200, 300);
81    println!("triple3: {}", triple3);
82    println!(
83        "  first: {}, second: {}, third: {}",
84        triple3.first(),
85        triple3.second(),
86        triple3.third()
87    );
88
89    // Map operations
90    let triple4 = triple3
91        .map_first(|x| x / 10)
92        .map_second(|x| x / 10)
93        .map_third(|x| x / 10);
94    println!("triple4 (after mapping): {}", triple4);
95
96    // Convert to tuple
97    let (f, s, t) = triple4.into_tuple();
98    println!("Converted to tuple: ({}, {}, {})", f, s, t);
99
100    println!("\n=== Advanced Usage ===\n");
101
102    // Pair with different types
103    let config = Pair {
104        first: "database_url",
105        second: "postgresql://localhost/mydb",
106    };
107    println!("Config: {}", config);
108
109    // Triple as a record
110    let record = Triple {
111        first: "user_001",
112        second: "Alice",
113        third: 25,
114    };
115    println!(
116        "User Record: ID={}, Name={}, Age={}",
117        record.first, record.second, record.third
118    );
119
120    // Nested structures
121    let nested = Pair {
122        first: Pair::new(1, 2),
123        second: Triple::new(3, 4, 5),
124    };
125    println!(
126        "Nested: pair=({}, {}), triple=({}, {}, {})",
127        nested.first.first,
128        nested.first.second,
129        nested.second.first,
130        nested.second.second,
131        nested.second.third
132    );
133}

Trait Implementations§

Source§

impl<F: Clone, S: Clone, T: Clone> Clone for Triple<F, S, T>

Source§

fn clone(&self) -> Triple<F, S, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug, S: Debug, T: Debug> Debug for Triple<F, S, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F: Default, S: Default, T: Default> Default for Triple<F, S, T>

Source§

fn default() -> Triple<F, S, T>

Returns the “default value” for a type. Read more
Source§

impl<F: Display, S: Display, T: Display> Display for Triple<F, S, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F, S, T> From<(F, S, T)> for Triple<F, S, T>

Source§

fn from(tuple: (F, S, T)) -> Self

Creates a Triple from a tuple.

§Examples
use qubit_common::Triple;

let triple: Triple<i32, &str, bool> = (1, "hello", true).into();
assert_eq!(triple.first, 1);
assert_eq!(triple.second, "hello");
assert_eq!(triple.third, true);
Source§

impl<F, S, T> From<Triple<F, S, T>> for (F, S, T)

Source§

fn from(triple: Triple<F, S, T>) -> Self

Converts a Triple into a tuple.

§Examples
use qubit_common::Triple;

let triple = Triple::new(1, "hello", true);
let tuple: (i32, &str, bool) = triple.into();
assert_eq!(tuple, (1, "hello", true));
Source§

impl<F: Hash, S: Hash, T: Hash> Hash for Triple<F, S, T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<F: PartialEq, S: PartialEq, T: PartialEq> PartialEq for Triple<F, S, T>

Source§

fn eq(&self, other: &Triple<F, S, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<F: Copy, S: Copy, T: Copy> Copy for Triple<F, S, T>

Source§

impl<F: Eq, S: Eq, T: Eq> Eq for Triple<F, S, T>

Source§

impl<F, S, T> StructuralPartialEq for Triple<F, S, T>

Auto Trait Implementations§

§

impl<F, S, T> Freeze for Triple<F, S, T>
where F: Freeze, S: Freeze, T: Freeze,

§

impl<F, S, T> RefUnwindSafe for Triple<F, S, T>

§

impl<F, S, T> Send for Triple<F, S, T>
where F: Send, S: Send, T: Send,

§

impl<F, S, T> Sync for Triple<F, S, T>
where F: Sync, S: Sync, T: Sync,

§

impl<F, S, T> Unpin for Triple<F, S, T>
where F: Unpin, S: Unpin, T: Unpin,

§

impl<F, S, T> UnsafeUnpin for Triple<F, S, T>

§

impl<F, S, T> UnwindSafe for Triple<F, S, T>
where F: UnwindSafe, S: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoResult<T> for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.