HiveBrain v1.2.0
Get Started
← Back to all entries
patternrustMajor

Can I mark a function as deprecated?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
functiondeprecatedmarkcan

Problem

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?

Solution

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:

:6:5: 6:15 warning: use of deprecated item: please use `new_method` instead, #[warn(deprecated)] on by default
:6     old_method()
             ^~~~~~~~~~


You may find more information in the RFC.

Code Snippets

#[deprecated(since="0.5.0", note="please use `new_method` instead")]
pub fn 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()
             ^~~~~~~~~~

Context

Stack Overflow Q#30757914, score: 62

Revisions (0)

No revisions yet.