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 elementS- 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/Intotraits
§Differences
| Feature | Pair<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 matching | ✓ Pair { first, second } | ✓ (first, second) |
§When to Use Pair
Use Pair<F, S> when:
- You want more readable code with named fields (
.first,.secondvs.0,.1) - You need transformation methods like
map_first()orswap() - 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: FThe first element of the pair
second: SThe second element of the pair
Implementations§
Source§impl<F, S> Pair<F, S>
impl<F, S> Pair<F, S>
Sourcepub fn new(first: F, second: S) -> Self
pub fn new(first: F, second: S) -> Self
Creates a new Pair with the given values.
§Arguments
first- The first elementsecond- 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}Sourcepub fn into_tuple(self) -> (F, S)
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}Sourcepub fn first(&self) -> &F
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}Sourcepub fn second(&self) -> &S
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}Sourcepub fn first_mut(&mut self) -> &mut F
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);Sourcepub fn second_mut(&mut self) -> &mut S
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);Sourcepub fn map_first<F2, Fn>(self, f: Fn) -> Pair<F2, S>where
Fn: FnOnce(F) -> F2,
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}Sourcepub fn map_second<S2, Fn>(self, f: Fn) -> Pair<F, S2>where
Fn: FnOnce(S) -> S2,
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}Sourcepub fn swap(self) -> Pair<S, F>
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§
impl<F: Copy, S: Copy> Copy for Pair<F, S>
impl<F: Eq, S: Eq> Eq for Pair<F, S>
impl<F, S> StructuralPartialEq for Pair<F, S>
Auto Trait Implementations§
impl<F, S> Freeze for Pair<F, S>
impl<F, S> RefUnwindSafe for Pair<F, S>where
F: RefUnwindSafe,
S: RefUnwindSafe,
impl<F, S> Send for Pair<F, S>
impl<F, S> Sync for Pair<F, S>
impl<F, S> Unpin for Pair<F, S>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more