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 elementS- The type of the second elementT- 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/Intotraits - Both have similar memory layout and zero runtime overhead
§Differences
| Feature | Native Tuple (F, S, T) | Triple<F, S, T> |
|---|---|---|
| Field Access | .0, .1, .2 (positional) | .first, .second, .third (named) |
| Semantics | Anonymous, positional | Self-documenting, semantic |
| Methods | Limited built-in methods | Rich API (map, getters, setters) |
| Readability | Less clear in complex code | More expressive and maintainable |
| Type Alias | Hard to create meaningful aliases | Easy to create domain-specific types |
§When to Use Triple vs Native Tuples
Use Triple when:
- Field names improve code readability (e.g.,
triple.firstvstuple.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 prism3_core::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: FThe first element of the triple
second: SThe second element of the triple
third: TThe third element of the triple
Implementations§
Source§impl<F, S, T> Triple<F, S, T>
impl<F, S, T> Triple<F, S, T>
Sourcepub fn new(first: F, second: S, third: T) -> Self
pub fn new(first: F, second: S, third: T) -> Self
Creates a new Triple with the given values.
§Arguments
first- The first elementsecond- The second elementthird- The third element
§Examples
use prism3_core::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?
19fn main() {
20 println!("=== Pair Demo ===\n");
21
22 // Create Pair using new method
23 let pair1 = Pair::new("name", "Alice");
24 println!("pair1 (using new): {}", pair1);
25
26 // Create Pair using struct literal (public fields)
27 let mut pair2 = Pair {
28 first: 1,
29 second: 100,
30 };
31 println!("pair2 (using struct literal): {}", pair2);
32
33 // Direct field access (public fields)
34 pair2.first = 42;
35 pair2.second = 200;
36 println!("pair2 (after modification): {}", pair2);
37
38 // Using methods
39 let pair3 = Pair::new(10, 20);
40 println!("pair3: {}", pair3);
41 println!(" first: {}, second: {}", pair3.first(), pair3.second());
42
43 // Map operations
44 let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
45 println!("pair4 (after mapping): {}", pair4);
46
47 // Swap
48 let pair5 = Pair::new("key", 123);
49 let swapped = pair5.swap();
50 println!("pair5: {}, swapped: {}", pair5, swapped);
51
52 // Convert to tuple
53 let (f, s) = pair5.into_tuple();
54 println!("Converted to tuple: ({}, {})", f, s);
55
56 println!("\n=== Triple Demo ===\n");
57
58 // Create Triple using new method
59 let triple1 = Triple::new("Alice", 30, true);
60 println!("triple1 (using new): {}", triple1);
61
62 // Create Triple using struct literal (public fields)
63 let mut triple2 = Triple {
64 first: 1,
65 second: 2,
66 third: 3,
67 };
68 println!("triple2 (using struct literal): {}", triple2);
69
70 // Direct field access (public fields)
71 triple2.first = 10;
72 triple2.second = 20;
73 triple2.third = 30;
74 println!("triple2 (after modification): {}", triple2);
75
76 // Using methods
77 let triple3 = Triple::new(100, 200, 300);
78 println!("triple3: {}", triple3);
79 println!(
80 " first: {}, second: {}, third: {}",
81 triple3.first(),
82 triple3.second(),
83 triple3.third()
84 );
85
86 // Map operations
87 let triple4 = triple3
88 .map_first(|x| x / 10)
89 .map_second(|x| x / 10)
90 .map_third(|x| x / 10);
91 println!("triple4 (after mapping): {}", triple4);
92
93 // Convert to tuple
94 let (f, s, t) = triple4.into_tuple();
95 println!("Converted to tuple: ({}, {}, {})", f, s, t);
96
97 println!("\n=== Advanced Usage ===\n");
98
99 // Pair with different types
100 let config = Pair {
101 first: "database_url",
102 second: "postgresql://localhost/mydb",
103 };
104 println!("Config: {}", config);
105
106 // Triple as a record
107 let record = Triple {
108 first: "user_001",
109 second: "Alice",
110 third: 25,
111 };
112 println!(
113 "User Record: ID={}, Name={}, Age={}",
114 record.first, record.second, record.third
115 );
116
117 // Nested structures
118 let nested = Pair {
119 first: Pair::new(1, 2),
120 second: Triple::new(3, 4, 5),
121 };
122 println!(
123 "Nested: pair=({}, {}), triple=({}, {}, {})",
124 nested.first.first,
125 nested.first.second,
126 nested.second.first,
127 nested.second.second,
128 nested.second.third
129 );
130}Sourcepub fn into_tuple(self) -> (F, S, T)
pub fn into_tuple(self) -> (F, S, T)
Consumes the triple and returns a tuple of its elements.
§Examples
use prism3_core::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?
19fn main() {
20 println!("=== Pair Demo ===\n");
21
22 // Create Pair using new method
23 let pair1 = Pair::new("name", "Alice");
24 println!("pair1 (using new): {}", pair1);
25
26 // Create Pair using struct literal (public fields)
27 let mut pair2 = Pair {
28 first: 1,
29 second: 100,
30 };
31 println!("pair2 (using struct literal): {}", pair2);
32
33 // Direct field access (public fields)
34 pair2.first = 42;
35 pair2.second = 200;
36 println!("pair2 (after modification): {}", pair2);
37
38 // Using methods
39 let pair3 = Pair::new(10, 20);
40 println!("pair3: {}", pair3);
41 println!(" first: {}, second: {}", pair3.first(), pair3.second());
42
43 // Map operations
44 let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
45 println!("pair4 (after mapping): {}", pair4);
46
47 // Swap
48 let pair5 = Pair::new("key", 123);
49 let swapped = pair5.swap();
50 println!("pair5: {}, swapped: {}", pair5, swapped);
51
52 // Convert to tuple
53 let (f, s) = pair5.into_tuple();
54 println!("Converted to tuple: ({}, {})", f, s);
55
56 println!("\n=== Triple Demo ===\n");
57
58 // Create Triple using new method
59 let triple1 = Triple::new("Alice", 30, true);
60 println!("triple1 (using new): {}", triple1);
61
62 // Create Triple using struct literal (public fields)
63 let mut triple2 = Triple {
64 first: 1,
65 second: 2,
66 third: 3,
67 };
68 println!("triple2 (using struct literal): {}", triple2);
69
70 // Direct field access (public fields)
71 triple2.first = 10;
72 triple2.second = 20;
73 triple2.third = 30;
74 println!("triple2 (after modification): {}", triple2);
75
76 // Using methods
77 let triple3 = Triple::new(100, 200, 300);
78 println!("triple3: {}", triple3);
79 println!(
80 " first: {}, second: {}, third: {}",
81 triple3.first(),
82 triple3.second(),
83 triple3.third()
84 );
85
86 // Map operations
87 let triple4 = triple3
88 .map_first(|x| x / 10)
89 .map_second(|x| x / 10)
90 .map_third(|x| x / 10);
91 println!("triple4 (after mapping): {}", triple4);
92
93 // Convert to tuple
94 let (f, s, t) = triple4.into_tuple();
95 println!("Converted to tuple: ({}, {}, {})", f, s, t);
96
97 println!("\n=== Advanced Usage ===\n");
98
99 // Pair with different types
100 let config = Pair {
101 first: "database_url",
102 second: "postgresql://localhost/mydb",
103 };
104 println!("Config: {}", config);
105
106 // Triple as a record
107 let record = Triple {
108 first: "user_001",
109 second: "Alice",
110 third: 25,
111 };
112 println!(
113 "User Record: ID={}, Name={}, Age={}",
114 record.first, record.second, record.third
115 );
116
117 // Nested structures
118 let nested = Pair {
119 first: Pair::new(1, 2),
120 second: Triple::new(3, 4, 5),
121 };
122 println!(
123 "Nested: pair=({}, {}), triple=({}, {}, {})",
124 nested.first.first,
125 nested.first.second,
126 nested.second.first,
127 nested.second.second,
128 nested.second.third
129 );
130}Sourcepub fn first(&self) -> &F
pub fn first(&self) -> &F
Returns a reference to the first element.
§Examples
use prism3_core::Triple;
let triple = Triple::new(1, 2, 3);
assert_eq!(triple.first(), &1);Examples found in repository?
19fn main() {
20 println!("=== Pair Demo ===\n");
21
22 // Create Pair using new method
23 let pair1 = Pair::new("name", "Alice");
24 println!("pair1 (using new): {}", pair1);
25
26 // Create Pair using struct literal (public fields)
27 let mut pair2 = Pair {
28 first: 1,
29 second: 100,
30 };
31 println!("pair2 (using struct literal): {}", pair2);
32
33 // Direct field access (public fields)
34 pair2.first = 42;
35 pair2.second = 200;
36 println!("pair2 (after modification): {}", pair2);
37
38 // Using methods
39 let pair3 = Pair::new(10, 20);
40 println!("pair3: {}", pair3);
41 println!(" first: {}, second: {}", pair3.first(), pair3.second());
42
43 // Map operations
44 let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
45 println!("pair4 (after mapping): {}", pair4);
46
47 // Swap
48 let pair5 = Pair::new("key", 123);
49 let swapped = pair5.swap();
50 println!("pair5: {}, swapped: {}", pair5, swapped);
51
52 // Convert to tuple
53 let (f, s) = pair5.into_tuple();
54 println!("Converted to tuple: ({}, {})", f, s);
55
56 println!("\n=== Triple Demo ===\n");
57
58 // Create Triple using new method
59 let triple1 = Triple::new("Alice", 30, true);
60 println!("triple1 (using new): {}", triple1);
61
62 // Create Triple using struct literal (public fields)
63 let mut triple2 = Triple {
64 first: 1,
65 second: 2,
66 third: 3,
67 };
68 println!("triple2 (using struct literal): {}", triple2);
69
70 // Direct field access (public fields)
71 triple2.first = 10;
72 triple2.second = 20;
73 triple2.third = 30;
74 println!("triple2 (after modification): {}", triple2);
75
76 // Using methods
77 let triple3 = Triple::new(100, 200, 300);
78 println!("triple3: {}", triple3);
79 println!(
80 " first: {}, second: {}, third: {}",
81 triple3.first(),
82 triple3.second(),
83 triple3.third()
84 );
85
86 // Map operations
87 let triple4 = triple3
88 .map_first(|x| x / 10)
89 .map_second(|x| x / 10)
90 .map_third(|x| x / 10);
91 println!("triple4 (after mapping): {}", triple4);
92
93 // Convert to tuple
94 let (f, s, t) = triple4.into_tuple();
95 println!("Converted to tuple: ({}, {}, {})", f, s, t);
96
97 println!("\n=== Advanced Usage ===\n");
98
99 // Pair with different types
100 let config = Pair {
101 first: "database_url",
102 second: "postgresql://localhost/mydb",
103 };
104 println!("Config: {}", config);
105
106 // Triple as a record
107 let record = Triple {
108 first: "user_001",
109 second: "Alice",
110 third: 25,
111 };
112 println!(
113 "User Record: ID={}, Name={}, Age={}",
114 record.first, record.second, record.third
115 );
116
117 // Nested structures
118 let nested = Pair {
119 first: Pair::new(1, 2),
120 second: Triple::new(3, 4, 5),
121 };
122 println!(
123 "Nested: pair=({}, {}), triple=({}, {}, {})",
124 nested.first.first,
125 nested.first.second,
126 nested.second.first,
127 nested.second.second,
128 nested.second.third
129 );
130}Sourcepub fn second(&self) -> &S
pub fn second(&self) -> &S
Returns a reference to the second element.
§Examples
use prism3_core::Triple;
let triple = Triple::new(1, 2, 3);
assert_eq!(triple.second(), &2);Examples found in repository?
19fn main() {
20 println!("=== Pair Demo ===\n");
21
22 // Create Pair using new method
23 let pair1 = Pair::new("name", "Alice");
24 println!("pair1 (using new): {}", pair1);
25
26 // Create Pair using struct literal (public fields)
27 let mut pair2 = Pair {
28 first: 1,
29 second: 100,
30 };
31 println!("pair2 (using struct literal): {}", pair2);
32
33 // Direct field access (public fields)
34 pair2.first = 42;
35 pair2.second = 200;
36 println!("pair2 (after modification): {}", pair2);
37
38 // Using methods
39 let pair3 = Pair::new(10, 20);
40 println!("pair3: {}", pair3);
41 println!(" first: {}, second: {}", pair3.first(), pair3.second());
42
43 // Map operations
44 let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
45 println!("pair4 (after mapping): {}", pair4);
46
47 // Swap
48 let pair5 = Pair::new("key", 123);
49 let swapped = pair5.swap();
50 println!("pair5: {}, swapped: {}", pair5, swapped);
51
52 // Convert to tuple
53 let (f, s) = pair5.into_tuple();
54 println!("Converted to tuple: ({}, {})", f, s);
55
56 println!("\n=== Triple Demo ===\n");
57
58 // Create Triple using new method
59 let triple1 = Triple::new("Alice", 30, true);
60 println!("triple1 (using new): {}", triple1);
61
62 // Create Triple using struct literal (public fields)
63 let mut triple2 = Triple {
64 first: 1,
65 second: 2,
66 third: 3,
67 };
68 println!("triple2 (using struct literal): {}", triple2);
69
70 // Direct field access (public fields)
71 triple2.first = 10;
72 triple2.second = 20;
73 triple2.third = 30;
74 println!("triple2 (after modification): {}", triple2);
75
76 // Using methods
77 let triple3 = Triple::new(100, 200, 300);
78 println!("triple3: {}", triple3);
79 println!(
80 " first: {}, second: {}, third: {}",
81 triple3.first(),
82 triple3.second(),
83 triple3.third()
84 );
85
86 // Map operations
87 let triple4 = triple3
88 .map_first(|x| x / 10)
89 .map_second(|x| x / 10)
90 .map_third(|x| x / 10);
91 println!("triple4 (after mapping): {}", triple4);
92
93 // Convert to tuple
94 let (f, s, t) = triple4.into_tuple();
95 println!("Converted to tuple: ({}, {}, {})", f, s, t);
96
97 println!("\n=== Advanced Usage ===\n");
98
99 // Pair with different types
100 let config = Pair {
101 first: "database_url",
102 second: "postgresql://localhost/mydb",
103 };
104 println!("Config: {}", config);
105
106 // Triple as a record
107 let record = Triple {
108 first: "user_001",
109 second: "Alice",
110 third: 25,
111 };
112 println!(
113 "User Record: ID={}, Name={}, Age={}",
114 record.first, record.second, record.third
115 );
116
117 // Nested structures
118 let nested = Pair {
119 first: Pair::new(1, 2),
120 second: Triple::new(3, 4, 5),
121 };
122 println!(
123 "Nested: pair=({}, {}), triple=({}, {}, {})",
124 nested.first.first,
125 nested.first.second,
126 nested.second.first,
127 nested.second.second,
128 nested.second.third
129 );
130}Sourcepub fn third(&self) -> &T
pub fn third(&self) -> &T
Returns a reference to the third element.
§Examples
use prism3_core::Triple;
let triple = Triple::new(1, 2, 3);
assert_eq!(triple.third(), &3);Examples found in repository?
19fn main() {
20 println!("=== Pair Demo ===\n");
21
22 // Create Pair using new method
23 let pair1 = Pair::new("name", "Alice");
24 println!("pair1 (using new): {}", pair1);
25
26 // Create Pair using struct literal (public fields)
27 let mut pair2 = Pair {
28 first: 1,
29 second: 100,
30 };
31 println!("pair2 (using struct literal): {}", pair2);
32
33 // Direct field access (public fields)
34 pair2.first = 42;
35 pair2.second = 200;
36 println!("pair2 (after modification): {}", pair2);
37
38 // Using methods
39 let pair3 = Pair::new(10, 20);
40 println!("pair3: {}", pair3);
41 println!(" first: {}, second: {}", pair3.first(), pair3.second());
42
43 // Map operations
44 let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
45 println!("pair4 (after mapping): {}", pair4);
46
47 // Swap
48 let pair5 = Pair::new("key", 123);
49 let swapped = pair5.swap();
50 println!("pair5: {}, swapped: {}", pair5, swapped);
51
52 // Convert to tuple
53 let (f, s) = pair5.into_tuple();
54 println!("Converted to tuple: ({}, {})", f, s);
55
56 println!("\n=== Triple Demo ===\n");
57
58 // Create Triple using new method
59 let triple1 = Triple::new("Alice", 30, true);
60 println!("triple1 (using new): {}", triple1);
61
62 // Create Triple using struct literal (public fields)
63 let mut triple2 = Triple {
64 first: 1,
65 second: 2,
66 third: 3,
67 };
68 println!("triple2 (using struct literal): {}", triple2);
69
70 // Direct field access (public fields)
71 triple2.first = 10;
72 triple2.second = 20;
73 triple2.third = 30;
74 println!("triple2 (after modification): {}", triple2);
75
76 // Using methods
77 let triple3 = Triple::new(100, 200, 300);
78 println!("triple3: {}", triple3);
79 println!(
80 " first: {}, second: {}, third: {}",
81 triple3.first(),
82 triple3.second(),
83 triple3.third()
84 );
85
86 // Map operations
87 let triple4 = triple3
88 .map_first(|x| x / 10)
89 .map_second(|x| x / 10)
90 .map_third(|x| x / 10);
91 println!("triple4 (after mapping): {}", triple4);
92
93 // Convert to tuple
94 let (f, s, t) = triple4.into_tuple();
95 println!("Converted to tuple: ({}, {}, {})", f, s, t);
96
97 println!("\n=== Advanced Usage ===\n");
98
99 // Pair with different types
100 let config = Pair {
101 first: "database_url",
102 second: "postgresql://localhost/mydb",
103 };
104 println!("Config: {}", config);
105
106 // Triple as a record
107 let record = Triple {
108 first: "user_001",
109 second: "Alice",
110 third: 25,
111 };
112 println!(
113 "User Record: ID={}, Name={}, Age={}",
114 record.first, record.second, record.third
115 );
116
117 // Nested structures
118 let nested = Pair {
119 first: Pair::new(1, 2),
120 second: Triple::new(3, 4, 5),
121 };
122 println!(
123 "Nested: pair=({}, {}), triple=({}, {}, {})",
124 nested.first.first,
125 nested.first.second,
126 nested.second.first,
127 nested.second.second,
128 nested.second.third
129 );
130}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 prism3_core::Triple;
let mut triple = Triple::new(1, 2, 3);
*triple.first_mut() = 10;
assert_eq!(triple.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 prism3_core::Triple;
let mut triple = Triple::new(1, 2, 3);
*triple.second_mut() = 20;
assert_eq!(triple.second, 20);Sourcepub fn third_mut(&mut self) -> &mut T
pub fn third_mut(&mut self) -> &mut T
Returns a mutable reference to the third element.
§Examples
use prism3_core::Triple;
let mut triple = Triple::new(1, 2, 3);
*triple.third_mut() = 30;
assert_eq!(triple.third, 30);Sourcepub fn map_first<F2, Fn>(self, f: Fn) -> Triple<F2, S, T>where
Fn: FnOnce(F) -> F2,
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 prism3_core::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?
19fn main() {
20 println!("=== Pair Demo ===\n");
21
22 // Create Pair using new method
23 let pair1 = Pair::new("name", "Alice");
24 println!("pair1 (using new): {}", pair1);
25
26 // Create Pair using struct literal (public fields)
27 let mut pair2 = Pair {
28 first: 1,
29 second: 100,
30 };
31 println!("pair2 (using struct literal): {}", pair2);
32
33 // Direct field access (public fields)
34 pair2.first = 42;
35 pair2.second = 200;
36 println!("pair2 (after modification): {}", pair2);
37
38 // Using methods
39 let pair3 = Pair::new(10, 20);
40 println!("pair3: {}", pair3);
41 println!(" first: {}, second: {}", pair3.first(), pair3.second());
42
43 // Map operations
44 let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
45 println!("pair4 (after mapping): {}", pair4);
46
47 // Swap
48 let pair5 = Pair::new("key", 123);
49 let swapped = pair5.swap();
50 println!("pair5: {}, swapped: {}", pair5, swapped);
51
52 // Convert to tuple
53 let (f, s) = pair5.into_tuple();
54 println!("Converted to tuple: ({}, {})", f, s);
55
56 println!("\n=== Triple Demo ===\n");
57
58 // Create Triple using new method
59 let triple1 = Triple::new("Alice", 30, true);
60 println!("triple1 (using new): {}", triple1);
61
62 // Create Triple using struct literal (public fields)
63 let mut triple2 = Triple {
64 first: 1,
65 second: 2,
66 third: 3,
67 };
68 println!("triple2 (using struct literal): {}", triple2);
69
70 // Direct field access (public fields)
71 triple2.first = 10;
72 triple2.second = 20;
73 triple2.third = 30;
74 println!("triple2 (after modification): {}", triple2);
75
76 // Using methods
77 let triple3 = Triple::new(100, 200, 300);
78 println!("triple3: {}", triple3);
79 println!(
80 " first: {}, second: {}, third: {}",
81 triple3.first(),
82 triple3.second(),
83 triple3.third()
84 );
85
86 // Map operations
87 let triple4 = triple3
88 .map_first(|x| x / 10)
89 .map_second(|x| x / 10)
90 .map_third(|x| x / 10);
91 println!("triple4 (after mapping): {}", triple4);
92
93 // Convert to tuple
94 let (f, s, t) = triple4.into_tuple();
95 println!("Converted to tuple: ({}, {}, {})", f, s, t);
96
97 println!("\n=== Advanced Usage ===\n");
98
99 // Pair with different types
100 let config = Pair {
101 first: "database_url",
102 second: "postgresql://localhost/mydb",
103 };
104 println!("Config: {}", config);
105
106 // Triple as a record
107 let record = Triple {
108 first: "user_001",
109 second: "Alice",
110 third: 25,
111 };
112 println!(
113 "User Record: ID={}, Name={}, Age={}",
114 record.first, record.second, record.third
115 );
116
117 // Nested structures
118 let nested = Pair {
119 first: Pair::new(1, 2),
120 second: Triple::new(3, 4, 5),
121 };
122 println!(
123 "Nested: pair=({}, {}), triple=({}, {}, {})",
124 nested.first.first,
125 nested.first.second,
126 nested.second.first,
127 nested.second.second,
128 nested.second.third
129 );
130}Sourcepub fn map_second<S2, Fn>(self, f: Fn) -> Triple<F, S2, T>where
Fn: FnOnce(S) -> S2,
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 prism3_core::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?
19fn main() {
20 println!("=== Pair Demo ===\n");
21
22 // Create Pair using new method
23 let pair1 = Pair::new("name", "Alice");
24 println!("pair1 (using new): {}", pair1);
25
26 // Create Pair using struct literal (public fields)
27 let mut pair2 = Pair {
28 first: 1,
29 second: 100,
30 };
31 println!("pair2 (using struct literal): {}", pair2);
32
33 // Direct field access (public fields)
34 pair2.first = 42;
35 pair2.second = 200;
36 println!("pair2 (after modification): {}", pair2);
37
38 // Using methods
39 let pair3 = Pair::new(10, 20);
40 println!("pair3: {}", pair3);
41 println!(" first: {}, second: {}", pair3.first(), pair3.second());
42
43 // Map operations
44 let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
45 println!("pair4 (after mapping): {}", pair4);
46
47 // Swap
48 let pair5 = Pair::new("key", 123);
49 let swapped = pair5.swap();
50 println!("pair5: {}, swapped: {}", pair5, swapped);
51
52 // Convert to tuple
53 let (f, s) = pair5.into_tuple();
54 println!("Converted to tuple: ({}, {})", f, s);
55
56 println!("\n=== Triple Demo ===\n");
57
58 // Create Triple using new method
59 let triple1 = Triple::new("Alice", 30, true);
60 println!("triple1 (using new): {}", triple1);
61
62 // Create Triple using struct literal (public fields)
63 let mut triple2 = Triple {
64 first: 1,
65 second: 2,
66 third: 3,
67 };
68 println!("triple2 (using struct literal): {}", triple2);
69
70 // Direct field access (public fields)
71 triple2.first = 10;
72 triple2.second = 20;
73 triple2.third = 30;
74 println!("triple2 (after modification): {}", triple2);
75
76 // Using methods
77 let triple3 = Triple::new(100, 200, 300);
78 println!("triple3: {}", triple3);
79 println!(
80 " first: {}, second: {}, third: {}",
81 triple3.first(),
82 triple3.second(),
83 triple3.third()
84 );
85
86 // Map operations
87 let triple4 = triple3
88 .map_first(|x| x / 10)
89 .map_second(|x| x / 10)
90 .map_third(|x| x / 10);
91 println!("triple4 (after mapping): {}", triple4);
92
93 // Convert to tuple
94 let (f, s, t) = triple4.into_tuple();
95 println!("Converted to tuple: ({}, {}, {})", f, s, t);
96
97 println!("\n=== Advanced Usage ===\n");
98
99 // Pair with different types
100 let config = Pair {
101 first: "database_url",
102 second: "postgresql://localhost/mydb",
103 };
104 println!("Config: {}", config);
105
106 // Triple as a record
107 let record = Triple {
108 first: "user_001",
109 second: "Alice",
110 third: 25,
111 };
112 println!(
113 "User Record: ID={}, Name={}, Age={}",
114 record.first, record.second, record.third
115 );
116
117 // Nested structures
118 let nested = Pair {
119 first: Pair::new(1, 2),
120 second: Triple::new(3, 4, 5),
121 };
122 println!(
123 "Nested: pair=({}, {}), triple=({}, {}, {})",
124 nested.first.first,
125 nested.first.second,
126 nested.second.first,
127 nested.second.second,
128 nested.second.third
129 );
130}Sourcepub fn map_third<T2, Fn>(self, f: Fn) -> Triple<F, S, T2>where
Fn: FnOnce(T) -> T2,
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 prism3_core::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?
19fn main() {
20 println!("=== Pair Demo ===\n");
21
22 // Create Pair using new method
23 let pair1 = Pair::new("name", "Alice");
24 println!("pair1 (using new): {}", pair1);
25
26 // Create Pair using struct literal (public fields)
27 let mut pair2 = Pair {
28 first: 1,
29 second: 100,
30 };
31 println!("pair2 (using struct literal): {}", pair2);
32
33 // Direct field access (public fields)
34 pair2.first = 42;
35 pair2.second = 200;
36 println!("pair2 (after modification): {}", pair2);
37
38 // Using methods
39 let pair3 = Pair::new(10, 20);
40 println!("pair3: {}", pair3);
41 println!(" first: {}, second: {}", pair3.first(), pair3.second());
42
43 // Map operations
44 let pair4 = pair3.map_first(|x| x * 2).map_second(|x| x * 3);
45 println!("pair4 (after mapping): {}", pair4);
46
47 // Swap
48 let pair5 = Pair::new("key", 123);
49 let swapped = pair5.swap();
50 println!("pair5: {}, swapped: {}", pair5, swapped);
51
52 // Convert to tuple
53 let (f, s) = pair5.into_tuple();
54 println!("Converted to tuple: ({}, {})", f, s);
55
56 println!("\n=== Triple Demo ===\n");
57
58 // Create Triple using new method
59 let triple1 = Triple::new("Alice", 30, true);
60 println!("triple1 (using new): {}", triple1);
61
62 // Create Triple using struct literal (public fields)
63 let mut triple2 = Triple {
64 first: 1,
65 second: 2,
66 third: 3,
67 };
68 println!("triple2 (using struct literal): {}", triple2);
69
70 // Direct field access (public fields)
71 triple2.first = 10;
72 triple2.second = 20;
73 triple2.third = 30;
74 println!("triple2 (after modification): {}", triple2);
75
76 // Using methods
77 let triple3 = Triple::new(100, 200, 300);
78 println!("triple3: {}", triple3);
79 println!(
80 " first: {}, second: {}, third: {}",
81 triple3.first(),
82 triple3.second(),
83 triple3.third()
84 );
85
86 // Map operations
87 let triple4 = triple3
88 .map_first(|x| x / 10)
89 .map_second(|x| x / 10)
90 .map_third(|x| x / 10);
91 println!("triple4 (after mapping): {}", triple4);
92
93 // Convert to tuple
94 let (f, s, t) = triple4.into_tuple();
95 println!("Converted to tuple: ({}, {}, {})", f, s, t);
96
97 println!("\n=== Advanced Usage ===\n");
98
99 // Pair with different types
100 let config = Pair {
101 first: "database_url",
102 second: "postgresql://localhost/mydb",
103 };
104 println!("Config: {}", config);
105
106 // Triple as a record
107 let record = Triple {
108 first: "user_001",
109 second: "Alice",
110 third: 25,
111 };
112 println!(
113 "User Record: ID={}, Name={}, Age={}",
114 record.first, record.second, record.third
115 );
116
117 // Nested structures
118 let nested = Pair {
119 first: Pair::new(1, 2),
120 second: Triple::new(3, 4, 5),
121 };
122 println!(
123 "Nested: pair=({}, {}), triple=({}, {}, {})",
124 nested.first.first,
125 nested.first.second,
126 nested.second.first,
127 nested.second.second,
128 nested.second.third
129 );
130}