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.