是否可以添加您自己的派生特征,或者这些特征是否由编译器修复? [英] Is it possible to add your own derivable traits, or are these fixed by the compiler?

查看:32
本文介绍了是否可以添加您自己的派生特征,或者这些特征是否由编译器修复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

derive 属性允许为数据结构自动实现某些特征.参考给出了例子:

The derive attribute allows certain traits to be automatically implemented for data structures. The reference gives the example:

#[derive(PartialEq, Clone)]
struct Foo<T> {
   a: i32,
   b: T
}

是否可以添加您自己的可派生特征,或者这些特征是否由编译器修复?

Is it possible to add your own derivable traits, or are these fixed by the compiler?

推荐答案

少数可衍生的特征是 在编译器中硬编码.

A small number of derivable traits are hard-coded in the compiler.

Rust 1.15 稳定过程宏,允许创建自定义 derive 属性.如果你需要在 1.15 之前支持稳定版本的 Rust,有一个 crate 使用宏hackery 来派生自定义特征.

Rust 1.15 stabilized procedural macros which allow for the creation of custom derive attributes. If you need to support stable versions of Rust before 1.15, there is a crate that uses macro hackery to derive custom traits.

这篇关于是否可以添加您自己的派生特征,或者这些特征是否由编译器修复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆