Use of undeclared crate or module `rustls`

Searching with the error encountered, but I do not find solution that fixes my problem. So here is mine.

I create a project call myapp by cargo new myapp with following content.

# Cargo.toml
[package]
name = "myapp"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rcgen = "0.12.1"
rustls = { version = "0.21.0", default-features = false, features = ["quic"], optional = true }

[features]
rustls = ["dep:rustls"]

# src/main.rs
fn f() {
  let addr = "localhost:8080";
  let cert = rcgen::generate_simple_self_signed(vec![addr.into()]).unwrap();
  let der_fmt = cert.serialize_der().unwrap();
  let pkcs8_fmt = cert.serialize_private_key_der();
  let private_key = rustls::PrivateKey(pkcs8_fmt); // error happened at this line
}

fn main() {
    f();
}

I add the crate with cargo add rustls, that returns following message in the console.

    Updating crates.io index
      Adding rustls v0.21.0 to optional dependencies.
             Features:
             + quic
             - dangerous_configuration
             - log
             - logging
             - read_buf
             - rustversion
             - secret_extraction
             - tls12

But building the app by cargo build throws following error.

   Compiling myapp v0.1.0 (/tmp/myapp)

error[E0433]: failed to resolve: use of undeclared crate or module rustls
--> src/main.rs:7:21
|
7 | let private_key = rustls::PrivateKey(pkcs8_fmt);
| ^^^^^^ use of undeclared crate or module rustls

For more information about this error, try `rustc --explain E0433`.
error: could not compile `myapp` (bin "myapp") due to 1 previous error

I also checked the name in rustls' release Cargo.toml, where the name is rustls without a problem.

What could go wrong? How to fix this? Thanks.

Dependencies can be marked “optional”, which means they will not be compiled by default.

src: Features - The Cargo Book

So the solution is to enable rustls somewhere, for example make it as one of default features.

[features]
default = ["rustls"] # add this line
rustls = ["dep:rustls"]

If you don't want it to be default, but still want RA to work on rustls features, check out RA's doc to know how to config features for a project.

3 Likes

This works, many thanks! One more question. I found inner attribute (#![cfg(features = "rustls")]) is also working in my case. The doc states that

Inner attributes, written with a bang (!) after the hash (#), apply to the item that the attribute is declared within.

So basically one can also declare to enable a feature by #!, followed by cfg(features={feature name}) pointing out which feature, i.e. feature name, needs to be active, right?

It's #![cfg(feature = "{feature name}")] just to be sure. feature, not features. But otherwise, yes, if you have annotated an item with such an attribute, it will only be conditionally compiled if the feature is enabled. See the Cargo book for more information on how features work.

3 Likes

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.