Skip to main content

Pair

Struct Pair 

Source
pub struct Pair<F, S> {
    pub first: F,
    pub second: S,
}
Expand description

A generic pair structure that holds two values.

§Type Parameters

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

§Pair vs. Tuple

While Rust’s native tuples (F, S) can also hold two values, Pair<F, S> provides several advantages:

§Similarities

  • Both can hold two values of different types
  • Both support pattern matching and destructuring
  • Both can be converted to each other via From/Into traits

§Differences

FeaturePair<F, S>(F, S)
Named fields✓ Uses .first and .second✗ Uses .0 and .1
Semantic clarity✓ More readable and self-documenting✗ Less clear what each element represents
Method chaining✓ Provides map_first(), map_second(), swap()✗ No built-in transformation methods
Mutable access✓ Provides first_mut() and second_mut()✓ Direct field mutation
Type aliases✓ Can create meaningful type aliases✓ Can create type aliases
Pattern matchingPair { first, second }(first, second)

§When to Use Pair

Use Pair<F, S> when:

  • You want more readable code with named fields (.first, .second vs .0, .1)
  • You need transformation methods like map_first() or swap()
  • You’re building a public API where clarity is important
  • You want to create semantic type aliases (e.g., type Coordinate = Pair<f64, f64>)

§When to Use Tuples

Use native tuples (F, S) when:

  • You need a quick, lightweight data structure for internal use
  • You’re returning multiple values from a function temporarily
  • You need to match Rust’s standard library APIs that use tuples
  • Performance is critical (though the difference is negligible in practice)

§Examples

use qubit_common::Pair;

let pair = Pair::new("name", 42);
assert_eq!(pair.first, "name");
assert_eq!(pair.second, 42);

let pair = Pair { first: 1, second: 2.5 };
assert_eq!(pair.first, 1);
assert_eq!(pair.second, 2.5);

Fields§

§first: F

The first element of the pair

§second: S

The second element of the pair

Implementations§

Source§

impl<F, S> Pair<F, S>

Source

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

Creates a new Pair with the given values.

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

let pair = Pair::new(1, "hello");
assert_eq!(pair.first, 1);
assert_eq!(pair.second, "hello");
Examples found in repository?
examples/util_demo.rs (line 26)
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)

Consumes the pair and returns a tuple of its elements.

§Examples
use qubit_common::Pair;

let pair = Pair::new(1, "hello");
let (first, second) = pair.into_tuple();
assert_eq!(first, 1);
assert_eq!(second, "hello");
Examples found in repository?
examples/util_demo.rs (line 56)
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::Pair;

let pair = Pair::new(1, 2);
assert_eq!(pair.first(), &1);
Examples found in repository?
examples/util_demo.rs (line 44)
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::Pair;

let pair = Pair::new(1, 2);
assert_eq!(pair.second(), &2);
Examples found in repository?
examples/util_demo.rs (line 44)
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::Pair;

let mut pair = Pair::new(1, 2);
*pair.first_mut() = 10;
assert_eq!(pair.first, 10);
Source

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

Returns a mutable reference to the second element.

§Examples
use qubit_common::Pair;

let mut pair = Pair::new(1, 2);
*pair.second_mut() = 20;
assert_eq!(pair.second, 20);
Source

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

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

§Examples
use qubit_common::Pair;

let pair = Pair::new(1, "hello");
let mapped = pair.map_first(|x| x * 2);
assert_eq!(mapped.first, 2);
assert_eq!(mapped.second, "hello");
Examples found in repository?
examples/util_demo.rs (line 47)
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) -> Pair<F, S2>
where Fn: FnOnce(S) -> S2,

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

§Examples
use qubit_common::Pair;

let pair = Pair::new(1, "hello");
let mapped = pair.map_second(|s| s.len());
assert_eq!(mapped.first, 1);
assert_eq!(mapped.second, 5);
Examples found in repository?
examples/util_demo.rs (line 47)
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 swap(self) -> Pair<S, F>

Swaps the elements of the pair.

§Examples
use qubit_common::Pair;

let pair = Pair::new(1, "hello");
let swapped = pair.swap();
assert_eq!(swapped.first, "hello");
assert_eq!(swapped.second, 1);
Examples found in repository?
examples/util_demo.rs (line 52)
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> Clone for Pair<F, S>

Source§

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

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> Debug for Pair<F, S>

Source§

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

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

impl<F: Default, S: Default> Default for Pair<F, S>

Source§

fn default() -> Pair<F, S>

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

impl<F: Display, S: Display> Display for Pair<F, S>

Source§

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

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

impl<F, S> From<(F, S)> for Pair<F, S>

Source§

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

Creates a Pair from a tuple.

§Examples
use qubit_common::Pair;

let pair: Pair<i32, &str> = (1, "hello").into();
assert_eq!(pair.first, 1);
assert_eq!(pair.second, "hello");
Source§

impl<F, S> From<Pair<F, S>> for (F, S)

Source§

fn from(pair: Pair<F, S>) -> Self

Converts a Pair into a tuple.

§Examples
use qubit_common::Pair;

let pair = Pair::new(1, "hello");
let tuple: (i32, &str) = pair.into();
assert_eq!(tuple, (1, "hello"));
Source§

impl<F: Hash, S: Hash> Hash for Pair<F, S>

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> PartialEq for Pair<F, S>

Source§

fn eq(&self, other: &Pair<F, S>) -> 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> Copy for Pair<F, S>

Source§

impl<F: Eq, S: Eq> Eq for Pair<F, S>

Source§

impl<F, S> StructuralPartialEq for Pair<F, S>

Auto Trait Implementations§

§

impl<F, S> Freeze for Pair<F, S>
where F: Freeze, S: Freeze,

§

impl<F, S> RefUnwindSafe for Pair<F, S>

§

impl<F, S> Send for Pair<F, S>
where F: Send, S: Send,

§

impl<F, S> Sync for Pair<F, S>
where F: Sync, S: Sync,

§

impl<F, S> Unpin for Pair<F, S>
where F: Unpin, S: Unpin,

§

impl<F, S> UnsafeUnpin for Pair<F, S>
where F: UnsafeUnpin, S: UnsafeUnpin,

§

impl<F, S> UnwindSafe for Pair<F, S>
where F: UnwindSafe, S: 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.