简体   繁体   中英

What is the recommended way to specify or import the Signed trait in Rust?

The Signed trait used to exist in the standard library , but it's gone now. A year ago, someone asked about implementing signed traits on reddit , but the thread didn't come to a conclusion, and the author moved on to ask again on the Rust language Discourse site with similar results .

Now there's a Signed trait in the num crate , but I have two concerns:

1) I don't really understand if importing a crate into my project has more overhead in the resulting file size, especially if I only use one trait from it.

2) Given the apparent instability of this kind of trait, I'm now leery of adding another dependency in my project if it's not going to stay stable and compatible.

What's the right course to chart here?

Use the crate. The compiler will ensure that an efficient binary is created. You probably won't notice any interesting change in your file size. Cargo will ensure that actually using the crate is easy.

The "stability" of the trait isn't an inherent property of the trait. Mostly, the usefulness of the trait is questionable. The standard library has to be conservative — anything present there has to be supported for the entire life of Rust 1.x (and there are no plans for a Rust 2.x). Adding stuff to the standard library is thus very constrained.

This is why crates are such first-class citizens. The decision is now up to the maintainers of the num crate for how long to support the trait. If they follow semver (and they should), it should be very obvious if they ever drop support for it. Also, once you have chosen a version of the crate to compile against, it will never change until you do a cargo update , so you are safe to use it.

In the worst of the worst cases, say the num crate decides to drop the trait. In that case, you can simply copy the implementation and produce your own crate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM