Search code examples
rustsemantic-versioning

How to use #[deprecated] for renaming public struct in backwards compatible way?


I see that I can use the #[deprecated] marker, but somehow it is not working in the following workflow:

  1. I renamed all my MyOldStruct to MyNewStruct in my library.
  2. Added a #deprecated note to pub use MyNewStruct as MyOldStruct.
pub struct MyNewStruct{};

#[deprecated]
pub use MyOldStruct as MyNewStruct;

Any ideas what might be going wrong here? I am viewing if it shows as deprecated in cargo docs.


Solution

  • Use a type alias:

    #[deprecated]
    pub type MyOldStruct = MyNewStruct;
    

    See it emit a warning on the playground.


    I will note that using a type alias for renaming and deprecating things doesn't work for everything: an aliased tuple struct can't use the new name construct a value as shown in the docs, and this method obviously doesn't work for traits since they aren't types and we don't have trait aliases yet.