Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. fn main() {
  2. println!("Hello, world!");
  3. }
  4.  
  5. struct Node<'a> {
  6. name: &'a str,
  7. }
  8.  
  9. fn find_or_insert<'a>(nodes: &'a mut Vec<Node<'a>>, name: &'a str) -> &'a Node<'a> {
  10. match nodes.iter().position(|n| n.name == name) {
  11. Some(n) => return &nodes[n],
  12. None => {
  13. let n = Node {
  14. name,
  15. };
  16. nodes.push(n);
  17. nodes.last().unwrap()
  18. }
  19. }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement