Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Clone)]
- struct Nestt(Option<::std::rc::Rc<Nestt>>);
- impl Nestt {
- pub fn depth(&self) -> usize {
- let mut q = ::std::borrow::Cow::Borrowed(self);
- let mut ctr = 0;
- loop {
- let cl = if let Some(ref x) = q.as_ref().0 {
- (**x).clone()
- } else {
- break;
- };
- q = ::std::borrow::Cow::Owned(cl);
- ctr+=1;
- }
- ctr
- }
- }
- fn main() {
- let n = Nestt(
- Some(std::rc::Rc::new(Nestt(
- Some(std::rc::Rc::new(Nestt(
- None
- )))
- )))
- );
- assert_eq!(n.depth(), 2);
- }
Add Comment
Please, Sign In to add comment