main.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. use assert_no_alloc::*;
  2. #[cfg(debug_assertions)]
  3. #[global_allocator]
  4. static A: AllocDisabler = AllocDisabler;
  5. fn main() {
  6. println!("Alloc is allowed. Let's allocate some memory...");
  7. let vec_can_allocate = vec![42; 10];
  8. println!("This will be executed if the above allocation succeeds: {vec_can_allocate:?}");
  9. println!();
  10. let fib5 = assert_no_alloc(|| {
  11. println!("Alloc is forbidden. Let's calculate something without memory allocations...");
  12. fn fib(n: u32) -> u32 {
  13. if n <= 1 {
  14. 1
  15. } else {
  16. fib(n - 1) + fib(n - 2)
  17. }
  18. }
  19. fib(5)
  20. });
  21. println!("\tSuccess, the 5th fibonacci number is {}", fib5);
  22. println!();
  23. assert_no_alloc(|| {
  24. println!("Alloc is forbidden. Let's allocate some memory...");
  25. let vec_cannot_allocate = vec![42; 100];
  26. println!("This will not be executed if the above allocation has aborted. {vec_cannot_allocate:?}");
  27. });
  28. println!("This will not be executed if the above allocation has aborted.");
  29. }