-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrusty.cc
More file actions
56 lines (45 loc) · 1.69 KB
/
rusty.cc
File metadata and controls
56 lines (45 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// @author Blake Freer
/// @date 2024-10-23
#include <format>
#include <iostream>
#include "rusty.h"
auto divide_option(float a, float b) -> rusty::Option<float> {
if (b == 0) return rusty::None;
return a / b;
}
auto divide_result(float a, float b) -> rusty::Result<float, std::string> {
if (b == 0) return rusty::Err("Divide by 0 error.");
return a / b;
}
int main() {
std::cout << "--- OPTION ---" << std::endl;
// match() returns string which is printed externally
auto option1 = rusty::match<float, std::string>(
divide_option(1, 4),
[](float f) { return std::format("Divide resulted in {:.3f}", f); },
[]() { return std::string{"Divide by 0 error."}; });
std::cout << option1 << std::endl;
// match() returns void, printing is internal
rusty::match<float, void>(
divide_option(1, 0),
[](float f) {
std::cout << std::format("Divide resulted in {:.3f}", f)
<< std::endl;
},
[]() { std::cout << "Divide by 0 error." << std::endl; });
std::cout << "--- RESULT ---" << std::endl;
// match() returns string which is printed externally
auto result1 = rusty::match<float, std::string, std::string>(
divide_result(1, 5),
[](auto f) { return std::format("Result = {:.3f}", f); },
[](auto e) { return e; });
std::cout << result1 << std::endl;
// match() returns void, printing is internal
rusty::match<float, std::string, void>(
divide_result(1, 0),
[](auto f) {
std::cout << std::format("Result = {:.3f}", f) << std::endl;
},
[](auto e) { std::cout << e << std::endl; });
return EXIT_SUCCESS;
}