fork download
  1. struct Droppable {
  2. name: &'static str, // '
  3. }
  4. impl Drop for Droppable {
  5. fn drop(&mut self) {
  6. println!("> Dropping {}", self.name);
  7. }
  8. }
  9. use std::fmt;
  10. impl fmt::Display for Droppable {
  11. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  12. write!(f, "{}", self.name)
  13. }
  14. }
  15. impl fmt::Debug for Droppable {
  16. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  17. write!(f, "{}", self.name)
  18. }
  19. }
  20. impl Clone for Droppable {
  21. fn clone(&self) -> Droppable { Droppable {name: self.name}}
  22. }
  23. fn main() {
  24. {
  25. let mut v = Vec::new();
  26. let a = Box::new(Droppable {name: "B"});
  27. v.push(Box::clone(&a));
  28. v.push(Box::clone(&a));
  29. v.push(a);
  30. println!("{:?}", v);
  31. }
  32. {
  33. use std::rc::Rc;
  34. let mut v = Vec::new();
  35. let a = Rc::new(Droppable {name: "R"});
  36. v.push(Rc::clone(&a));
  37. v.push(Rc::clone(&a));
  38. v.push(a);
  39. println!("{:?}", v);
  40. }
  41. }
  42.  
Success #stdin #stdout 0s 14864KB
stdin
Standard input is empty
stdout
[B, B, B]
> Dropping B
> Dropping B
> Dropping B
[R, R, R]
> Dropping R