Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. use std::borrow::Cow;
  2.  
  3. fn foo<'a, T: Into<Cow<'a, str>>>(arg: T) {
  4. let owned: String = match arg.into() {
  5. Cow::Borrowed(string) => {
  6. println!("allocates");
  7. string.to_owned()
  8. },
  9. Cow::Owned(string) => {
  10. println!("doesn't allocate");
  11. string
  12. },
  13. };
  14.  
  15. println!("{}", owned);
  16. }
  17.  
  18.  
  19. fn main() {
  20. let borrowed: &str = "test1";
  21. foo(borrowed);
  22. let owned: String = "test2".into();
  23. foo(owned);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement