rust 未解析的导入`serde`/`serde_json`

pgccezyw  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(175)

我构建项目时没有任何可见的错误,但在编译它时,我遇到错误E0432,它告诉我在Cargo.toml中声明serdeserde_json时找不到它们。我尝试从头开始重建项目,cargo checkcargo build多次,但我仍然卡住了
错误(S):

error[E0432]: unresolved import `serde`
 --> main.rs:1:5
  |
1 | use serde::{Deserialize, Serialize};
  |     ^^^^^ maybe a missing crate `serde`?

error[E0432]: unresolved import `serde_json`
 --> main.rs:2:5
  |
2 | use serde_json::Result;
  |     ^^^^^^^^^^ maybe a missing crate `serde_json`?

error: cannot determine resolution for the derive macro `Serialize`
 --> main.rs:4:10
  |
4 | #[derive(Serialize, Deserialize)]
  |          ^^^^^^^^^
  |
  = note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the derive macro `Deserialize`
 --> main.rs:4:21
  |
4 | #[derive(Serialize, Deserialize)]
  |                     ^^^^^^^^^^^
  |
  = note: import resolution is stuck, try simplifying macro imports

error[E0433]: failed to resolve: use of undeclared crate or module `serde_json`
  --> main.rs:14:32
   |
14 |     let config: ConfigStruct = serde_json::from_str(&config_data).unwrap();
   |                                ^^^^^^^^^^ use of undeclared crate or module `serde_json`

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0432, E0433.
For more information about an error, try `rustc --explain E0432`.

字符串
main.rs文件:

use serde::{Deserialize, Serialize};
use serde_json::Result;

#[derive(Serialize, Deserialize)]
struct ConfigStruct {
    playlist_name: String,
    replace_cover: bool,
    delete_songs: bool,
    songs_ids: Vec<String>,
}

fn get_config() -> Result<ConfigStruct> {
    let config_data = std::fs::read_to_string("../config.json").unwrap();
    let config: ConfigStruct = serde_json::from_str(&config_data).unwrap();
    Ok(config)
}

fn main() {
    let config = get_config().unwrap();

    println!("{}", config.playlist_name);
}


Cargo.toml文件:

[package]
name = "grrs"
version = "0.1.0"
edition = "2021"

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

[dependencies]
serde = {version = "1.0.144", features = ["derive"] }
serde_json = "1.0.85"


Cargo.lock文件:

# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "grrs"
version = "0.1.0"
dependencies = [
 "serde",
 "serde_json",
]

[[package]]
name = "itoa"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"

[[package]]
name = "proc-macro2"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
dependencies = [
 "unicode-ident",
]

[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"

[[package]]
name = "serde"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"
dependencies = [
 "serde_derive",
]

[[package]]
name = "serde_derive"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "serde_json"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
dependencies = [
 "itoa",
 "ryu",
 "serde",
]

[[package]]
name = "syn"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-ident",
]

[[package]]
name = "unicode-ident"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd"

ifsvaxew

ifsvaxew1#

您需要从serde机箱启用derive功能。
您的cargo.toml文件应该看起来像这样:

[package]
name = "grrs"
version = "0.1.0"
edition = "2021"

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

[dependencies]
serde = { version = "1.0.144", features = ["derive"] }
serde_json = "1.0.85"
rspotify = "0.11.5"

字符串
注意serde依赖声明中的 feature 键。

nom7f22z

nom7f22z2#

简单的解决方案:cargo add serde这就是你所需要的。
精确解:在cargo.toml处添加这些行

[dependencies]
leveldb = "0.8.6"
serde = "1.0.193"
serde_json = "1.0.108"

字符串
在此cargo runcargo build之后。

相关问题