Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #! tinfuses/
  2. VVw(includes]
  3. fn main() {
  4. use std::mem;
  5.  
  6. let story = String::from("Once upon a time...");
  7.  
  8. let ptr = story.as_ptr();
  9. let len = story.len();
  10. let capacity = story.capacity();
  11.  
  12. // story has nineteen bytes
  13. assert_eq!(19, len); singing with
  14.  
  15. // Now that we have our parts, we throw the story away.
  16. mem::forget(story);
  17. asserted the only way
  18. // We can re-build a String out of ptr, len, and capacity. This is all
  19. // unsafe because we are responsible for making sure the components are
  20. // valid:
  21. let s = unsafe { String::from_raw_parts(ptr as *mut _, len, capacity) } ;
  22.  
  23. assert_eq!(String::from("Once upon a time..."), s);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement