How can I overload the assignment operation in Rust?

后端 未结 2 1695
后悔当初
后悔当初 2020-12-07 01:56

There are lots of operations in std::ops, but there is nothing for a simple assignment.

I\'m coming from a C++ background, where there are copy constructor and assig

2条回答
  •  悲&欢浪女
    2020-12-07 02:17

    WRONG ANSWER

    As I said, I was confused :D. The answer to my question is to derive the Copy and Clone traits. I just have to add

    #[derive(Clone, Copy)]
    

    above my type definition; that way I can get my desired functionality.

    For a customized logic of assigning and copying, you can easily implement Copy and Clone yourself, instead of using derivation.

提交回复
热议问题