我可以将函数标记为已弃用吗? [英] Can I mark a function as deprecated?
问题描述
我想将函数/方法标记为已弃用.我尝试应用 deprecated
属性:
#[已弃用]fn old_way_of_doing_it() {
但这会产生错误:
<块引用>错误:不能在标准库之外使用稳定性属性
有没有办法让编译器警告我的库的使用者某个函数已被弃用?
我没有经验,但我正在考虑尝试使用编译器插件和自定义属性,但我想这将要求消费者也使用该插件,这可能是不合理的(或者对我来说可能是不合理的工作量)实施?)
作为一个出于好奇的附带问题,为什么 deprecated 属性仅适用于标准库?
自从 Rust 1.9.0(2016 年 5 月 26 日) 你可以使用 #[deprecated]
属性在你自己的箱子里(RFC 1270).语法是:
#[deprecated(since=0.5.0", note=请改用‘new_method’")]pub fn old_method() { ..;}
每当您使用 old_method
时,它都会抛出以下警告:
<anon>:6:5: 6:15 警告:使用已弃用的项目:请改用`new_method`,#[warn(deprecated)] 默认开启<anon>:6 old_method()^~~~~~~~~~
您可以在 RFC 中找到更多信息.
I'd like to mark functions/methods as deprecated. I tried to apply the deprecated
attribute:
#[deprecated]
fn old_way_of_doing_it() {
but this yields an error:
error: stability attributes may not be used outside of the standard library
Is there a way in which I can have the compiler warn a consumer of my library that a function is deprecated?
I have no experience, but I'm considering experimenting with compiler plugins and custom attributes, but I guess that would require the consumer to also use the plugin, which is maybe unreasonable (or may be an unreasonable amount of work for me to implement?)
As a side question out of curiosity, why is the deprecated attribute only applicable to the standard library?
Since Rust 1.9.0 (2016 May 26th) you can use the #[deprecated]
attribute in your own crates (RFC 1270). The syntax is:
#[deprecated(since="0.5.0", note="please use `new_method` instead")]
pub fn old_method() { ..; }
It will throw the following warning whenever you use old_method
:
<anon>:6:5: 6:15 warning: use of deprecated item: please use `new_method` instead, #[warn(deprecated)] on by default
<anon>:6 old_method()
^~~~~~~~~~
You may find more information in the RFC.
这篇关于我可以将函数标记为已弃用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!