Skip to content

rust的mem::swap的问题 #68

Open
@alexfanqi

Description

@alexfanqi

在看practical intro to macros时看到了这个操作

let mut swap_tmp = next;
for i in (0..2).rev()
    swap(&mut swap_tmp, &mut self.mem[i]);

作者声称用swap的好处是使得上面的代码也可以作用于non-copyable的类型。swap基于unsafe的操作,想问下swap这种使用方式是通常做法么?想不出对 无法copy的类型进行swap操作会用在哪里?储存引用/指针,然后直接swap引用不是更好

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions