How to create cargo package that is optimized

I need to publish a crate to crates.io. When i use cargo package, it builds an unoptimized package. I get this at the end of output

Finished dev [unoptimized + debuginfo] target(s) in 17.55s

The --release flag does not work with package. If i clean the target/packages directory and try to publish directly using publish, it still builds the unoptimized version as i get this at the end of output.

Finished dev [unoptimized + debuginfo] target(s) in 7.03s

Is there a way to publish an optimized build?

AFAIK, Cargo always publishes the source code and not the prebuilt packages. cargo package simply packages this code into some kind of archive to be used by crates.io, without any compilation.

2 Likes

Before publishing comes packaging. That is, a crate needs to be packaged and then published, which is what cargo package command does. It produces a .crate file that contains all the information required to manage the crate and build and install them elsewhere.

The .crate also contains the source code. To learn all the files that will packaged we can run cargo package --list.

AFIAK, the published package will be re-build by your users either in --release mode or development mode base on how they invoke their cargo build command.

1 Like

Thanks @Cerberuser and @prataprc.