Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. //I want to return a value in this function as efficiently as possible
  2. //If possible with no allocations to the heap
  3.  
  4. fn sum_all_items() -> Result<usize, &'static str> {
  5.  
  6. if let Ok(item) = load_all_items().map(|user| user.iter().count()) {
  7. // Get all items in a db using the `load_all_items() function` which returns a `Result<Vec<Items>, Err>
  8. //Use a `map` to extract the value
  9. //Use `.iter().count()` on the value to count all items of the Vec
  10. //Here a usize is returned from calculating
  11. Ok(item)
  12. }else {
  13.  
  14. //In case of an error, the values returned is a string
  15. Err("DB_READ_ERROR")
  16. }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement