-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbasic.rs
37 lines (30 loc) · 891 Bytes
/
basic.rs
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
//! This example demonstrates super basic memoization without any tracking.
//! While comemo goes way beyond this, it's of course also possible!
use comemo::memoize;
fn main() {
empty(); // [Miss] The cache is empty.
empty(); // [Hit] Always a hit from now on.
empty(); // [Hit] Always a hit from now on.
double(2); // [Miss] The cache is empty.
double(4); // [Miss] Different number.
double(2); // [Hit] Same number as initially.
sum(2, 4); // [Miss] The cache is empty.
sum(2, 3); // [Miss] Different numbers.
sum(2, 3); // [Hit] Same numbers
sum(4, 2); // [Miss] Different numbers.
}
/// Build a string.
#[memoize]
fn empty() -> String {
format!("The world is {}", "big")
}
/// Double a number.
#[memoize]
fn double(x: u32) -> u32 {
2 * x
}
/// Compute the sum of two numbers.
#[memoize]
fn sum(a: u32, b: u32) -> u32 {
a + b
}