Skip to content

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

Open
@ZhangHanDong

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函数,使用别的方法,该如何修正错误?

考察点: 所有权机制

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