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 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: 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 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?
examples/util_demo.rs (line 59)
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}
Source

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?
examples/util_demo.rs (line 94)
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}
Source

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?
examples/util_demo.rs (line 81)
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}
Source

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?
examples/util_demo.rs (line 82)
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}
Source

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?
examples/util_demo.rs (line 83)
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}
Source

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);
Source

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);
Source

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);
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 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?
examples/util_demo.rs (line 88)
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}
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 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?
examples/util_demo.rs (line 89)
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}
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 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?
examples/util_demo.rs (line 90)
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}

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 prism3_core::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 prism3_core::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> 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> 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.