Conditional `crate-type`

Please, help with understanding of the cargo feature crate-type.
I am developing a library that should be available for both TS (via WASM) and Rust.
Can I set this flag conditionally?

crate-type = ["cdylib", "rlib"]

So that it's only making a dynamic library when compiled into WASM and into Rust when specified as a dependency in another projects Cargo.toml.

1 Like

No, but you can use cargo rustc --crate-type $type in recent cargo.
See also cargo rustc - The Cargo Book

And what if it's a dependency in another crate? Do I specify it in the Cargo.toml there? Or do I keep it without that and only specify it when compiling into WASM?

I'm not sure what dependency you mean.
By default pure Rust dependencies will be compiled as a "lib" and rustc knows how to stitch everything together.
Only the final artifact would be a "cdylib" if that's what you need to produce.
It's also fine to simply have both in the crate-type option as you show and thus always produce both.
The new --crate-type option however would let you control to only built the "cdylib"


Thanks, I think now I get it.
But just to be sure: I have 2 crates: Rust binary and lib. If I specify

name = "some_lib"

crate-type = ["cdylib", "rlib"]

in the lib and have this in the binary

name = "some_bin"

some_lib = "*"

Will it work the same as if crate-type was not specified at all?

It should, yes.

1 Like

Thank you!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.