Skip to content

[Practice] 介绍id函数及其在代码中的技巧 #5

Description

@ZhangHanDong
#![allow(dead_code)]

fn main() {
    fn id<T>(x: T) -> T { x }
 
    struct List {
        next: Option<Box<List>>,
    }
    
    impl List {
        fn walk_the_list(&mut self) {
            let mut current = self;
            loop {
                match id(current).next {
                    None => return,
                    Some(ref mut inner) => current = inner,
                }
            }
        }
    }
}

如果不使用id函数,使用别的方法,该如何修正错误?

考察点: 所有权机制

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions