Memoize
A function whose results are remembered: calling it again with the same argument returns the stored result instead of computing it again.
The wrapped function receives the argument by reference and must be deterministic (and free
of side effects you rely on), since it runs only once per distinct argument. There is no
size limit: every distinct argument keeps its result until clear is called,
so do not feed it unbounded input.
Import
Section titled “Import”Cargo feature function (enabled by default). To compile only this module:
or in Cargo.toml:
Definition
Section titled “Definition”Examples
Section titled “Examples”Methods
Section titled “Methods”Wraps func.
Parameters
| Parameter | Type | Description |
|---|---|---|
func | F | The function whose results to remember. |
Returns
Self — A memoized version of func with an empty memory.
Calls the function with arg, or returns the remembered result for that argument.
Parameters
| Parameter | Type | Description |
|---|---|---|
arg | A | The argument to call the function with. |
Returns
R — The result for arg, computed at most once until clear.
The number of distinct arguments whose result is remembered.
Returns
usize — The number of stored results.
is_empty
Section titled “is_empty”Whether no result is remembered yet.
Returns
bool — true when nothing is stored.
Forgets every remembered result.
Returns
()
