MCPcopy Create free account
hub / github.com/Windfisch/rust-assert-no-alloc / main

Function main

examples/main.rs:7–34  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5static A: AllocDisabler = AllocDisabler;
6
7fn main() {
8 println!("Alloc is allowed. Let's allocate some memory...");
9 let mut vec_can_push = Vec::new();
10 vec_can_push.push(42);
11
12 println!();
13
14 let fib5 = assert_no_alloc(|| {
15 println!("Alloc is forbidden. Let's calculate something without memory allocations...");
16
17 fn fib(n: u32) -> u32 {
18 if n<=1 { 1 }
19 else { fib(n-1) + fib(n-2) }
20 }
21
22 fib(5)
23 });
24 println!("\tSuccess, the 5th fibonacci number is {}", fib5);
25 println!();
26
27 assert_no_alloc(|| {
28 println!("Alloc is forbidden. Let's allocate some memory...");
29 let mut vec_cannot_push = Vec::new();
30 vec_cannot_push.push(42); // panics
31 });
32
33 println!("This will not be executed if the above allocation has aborted.");
34}

Callers

nothing calls this directly

Calls 2

assert_no_allocFunction · 0.85
fibFunction · 0.85

Tested by

no test coverage detected