简体   繁体   English

为什么不能在箱子的源代码中添加main.rs并导入结构?

[英]Why can I not add a main.rs to a crate's source code and import a struct?

Why can I not clone a git project, add a main.rs and import one of the structs? 为什么我不能克隆git项目,添加main.rs并导入其中一个结构? The compiler complains that the import is unknown and if I make it known it complains about the library file can't be compiled. 编译器抱怨导入是未知的,如果我知道它,就抱怨无法编译库文件。

My files 我的文件

C:\Users\datan\proj\gitlab.kitware.com\rust-gitlab>dir
 Volume in drive C is OS
 Volume Serial Number is 9410-E4E7

 Directory of C:\Users\datan\proj\gitlab.kitware.com\rust-gitlab

2019-02-06  02:12    <DIR>          .
2019-02-06  02:12    <DIR>          ..
2019-02-06  02:04                20 .gitignore
2019-02-06  03:18    <DIR>          .idea
2019-02-06  02:05            63 519 Cargo.lock
2019-02-06  02:04               660 Cargo.toml
2019-02-06  02:12    <DIR>          cmake-build-debug
2019-02-06  02:04    <DIR>          data
2019-02-06  02:04            11 048 LICENSE-APACHE
2019-02-06  02:04             1 082 LICENSE-MIT
2019-02-06  02:04             2 720 README.md
2019-02-06  02:04               335 rustfmt.toml
2019-02-06  03:16    <DIR>          src
2019-02-06  02:11    <DIR>          target
               7 File(s)         79 384 bytes
               7 Dir(s)  55 351 238 656 bytes free

My "code": 我的“代码”:

use types::*;

fn main() {
    println!("Hello, rust-gitlab!");
    let repo_commit_detail: RepoCommitDetail = read_test_file("repo_commit_detail");
}

Structure 结构体

C:\Users\datan\proj\gitlab.kitware.com\rust-gitlab>tree .
Folder PATH listing for volume OS
Volume serial number is 00000242 9410:E4E7
C:\USERS\DATAN\PROJ\GITLAB.KITWARE.COM\RUST-GITLAB
├───.idea
│   └───codeStyles
├───cmake-build-debug
│   └───CMakeFiles
├───data
├───src
│   └───test
└───target
    └───debug
        ├───.fingerprint
        │   ├───adler32-008121952d6e723e
        │   ├───arrayvec-646ad0ece92a7d00
        │   ├───arrayvec-800298e92d8a6b00
        │   ├───arrayvec-8dcfe3021b2d6a8c
        │   ├───autocfg-d540d3308658af53
        │   ├───backtrace-4a5d8bbe58343768
        │   ├───backtrace-76389fa126a505ca
        │   ├───backtrace-a25120398233fbf2
        │   ├───base64-8605715cecafa1de
        │   ├───byteorder-6005d7caf2ed9454
        │   ├───byteorder-675368122e548ca7
        │   ├───byteorder-cacda78970cb326d
        │   ├───bytes-245614f42b19aebe
        │   ├───cfg-if-199b060bf8390609
        │   ├───chrono-5959f27994de03e0
        │   ├───crc32fast-0e880f27a0ce35a3
        │   ├───crc32fast-601ce62d000e21bd
        │   ├───crc32fast-9c59fdb01a618f87
        │   ├───crossbeam-424114c559f55459
        │   ├───crossbeam-channel-11d639148a522581
        │   ├───crossbeam-deque-52c98f99d1a02bdd
        │   ├───crossbeam-epoch-af8d62f026998a89
        │   ├───crossbeam-utils-0d1e4d7da8a9ef57
        │   ├───dtoa-24ecc23d181a18d5
        │   ├───either-2ee3e437ff6c971e
        │   ├───encoding_rs-d0dc5ad1a07eed3c
        │   ├───encoding_rs-da47aa074a980d52
        │   ├───encoding_rs-fd6e16943622578f
        │   ├───error-chain-bf90b77261c62193
        │   ├───fnv-aa2431fc6bc1ad66
        │   ├───futures-c6e67b10dc6edbd6
        │   ├───futures-cpupool-8e39e86f34ba679d
        │   ├───gitlab-08ffdd36690d90f8
        │   ├───gitlab-11fbba92a7cac036
        │   ├───gitlab-6a33ea7f6205065e
        │   ├───gitlab-ff4e8c6912a1992b
        │   ├───h2-0ee18ce773a0a89d
        │   ├───http-d2636b675bcf6465
        │   ├───httparse-3bfb6bbc4103b725
        │   ├───httparse-945236f44bbaae4b
        │   ├───httparse-a633d1291de4b063
        │   ├───hyper-d5c769dc0bac17de
        │   ├───hyper-tls-bb5d862976202e56
        │   ├───idna-eb455bf3c0c933b1
        │   ├───indexmap-fd3d2119afdeaa48
        │   ├───iovec-baee453073a2d5a5
        │   ├───itertools-ecd8c4d65670cbd4
        │   ├───itoa-2d8a8a209336a4b1
        │   ├───kernel32-sys-323344e9ba717bff
        │   ├───kernel32-sys-83eb403a9b94134f
        │   ├───kernel32-sys-8f6a7cb898839565
        │   ├───lazycell-c863022fb753ec04
        │   ├───lazy_static-ba3bb80e02027d7a
        │   ├───libc-058cf61bc83378d2
        │   ├───libc-0d6d3d189a8fcbc8
        │   ├───libc-7939fbb070556365
        │   ├───libflate-dda03a69fd4704db
        │   ├───lock_api-effbd268c74fb367
        │   ├───log-2694fa5b5ff478b9
        │   ├───matches-34ff768fe7fdff16
        │   ├───memoffset-5218595f33c3708e
        │   ├───mime-c20b452517077c0f
        │   ├───mime_guess-78f6fdcc75bfb801
        │   ├───mime_guess-b0cdb33e00444552
        │   ├───mime_guess-bbd685b703852b01
        │   ├───mio-97ff8160514f5116
        │   ├───miow-77aa8c8f9462ad96
        │   ├───native-tls-3753990a7317fe2a
        │   ├───native-tls-6abda051a7bbee28
        │   ├───native-tls-f608c79f4e18dacd
        │   ├───net2-508ac492f9b7b00d
        │   ├───nodrop-9f556cbcc9819baa
        │   ├───num-integer-3b8af9e09499eb17
        │   ├───num-integer-6591db06d92151f8
        │   ├───num-integer-f5b2b5fbdf9ab12f
        │   ├───num-traits-122ab06899abc72a
        │   ├───num-traits-5e3121b94e205820
        │   ├───num-traits-892c39b3d2eb30ab
        │   ├───num_cpus-e0b2ff8b8d59c6e1
        │   ├───owning_ref-bf183c7d773e0123
        │   ├───parking_lot-12a126bd707a9322
        │   ├───parking_lot_core-5118c128aa288759
        │   ├───parking_lot_core-d807a50d17f4c651
        │   ├───parking_lot_core-ff708f491c5bff4c
        │   ├───percent-encoding-2476bdc6c1b57ab9
        │   ├───phf-94566d3a79ef4ee4
        │   ├───phf_codegen-e81124e1842629f7
        │   ├───phf_generator-77be707e85bdbaab
        │   ├───phf_shared-83f1cdc4827f16e2
        │   ├───proc-macro2-3730dc9da7871eda
        │   ├───proc-macro2-490294cd99fdab5c
        │   ├───proc-macro2-a7ad7b68ac88e1e9
        │   ├───quote-0815e85bf26aa999
        │   ├───rand-36058bce56ceb9f0
        │   ├───rand-7bb4cfe73bf0dd1e
        │   ├───rand-e1919e05ce4b78ef
        │   ├───rand_chacha-5b135890bc288e05
        │   ├───rand_chacha-88fea0759acfb849
        │   ├───rand_chacha-ff6617e45043fb8d
        │   ├───rand_core-22f8b569c1c44c74
        │   ├───rand_core-44b0efb2e9232e43
        │   ├───rand_hc-6db16b100e560df6
        │   ├───rand_isaac-cd4ed7aeaa173fa8
        │   ├───rand_jitter-594b9541efdf7198
        │   ├───rand_os-408f5a001096e49e
        │   ├───rand_pcg-04b94dbba308ed6e
        │   ├───rand_pcg-7251ce262f199c3c
        │   ├───rand_pcg-ee7758f0896a3792
        │   ├───rand_xorshift-b4798ffeed80dc8e
        │   ├───reqwest-8cc9d5a46fc1cada
        │   ├───rustc-demangle-54becd0a270f9042
        │   ├───rustc_version-a903e7df435c73ab
        │   ├───ryu-5f4d3c0db59c3c54
        │   ├───ryu-6ba019180c4a274b
        │   ├───ryu-9235d9421bf627c7
        │   ├───schannel-8bdf6ec01769ef1c
        │   ├───scopeguard-56bbc3bacf26b561
        │   ├───semver-c5c93388824cc96e
        │   ├───semver-parser-654d61a88b761106
        │   ├───serde-d8d5485694af815f
        │   ├───serde-dad6256f5839031b
        │   ├───serde-fe431a9eda84f305
        │   ├───serde_derive-27a8dc8a7e741ec3
        │   ├───serde_json-54e6f40131bbe4f3
        │   ├───serde_urlencoded-164f04fa4cf91ece
        │   ├───siphasher-2144e89659a7ccab
        │   ├───slab-29ad5a838c81f6af
        │   ├───smallvec-8c0ee41d14e5bd2a
        │   ├───stable_deref_trait-6b5901538f6cb51d
        │   ├───string-726f5a2abd463420
        │   ├───syn-3788b283d77da292
        │   ├───time-2cbb174b24b625e6
        │   ├───tokio-current-thread-3f718ab37342974a
        │   ├───tokio-executor-e90e6a555413eaff
        │   ├───tokio-f4f06f03abb9066d
        │   ├───tokio-io-83563994d5ed4ae3
        │   ├───tokio-reactor-cee5f1c0b2993ee0
        │   ├───tokio-tcp-e384fdc1139f59d4
        │   ├───tokio-threadpool-f5fc7c392f4d3bcd
        │   ├───tokio-timer-baf35f3876a94a68
        │   ├───try-lock-5b05e80f1baf5381
        │   ├───unicase-24a369037c028ed4
        │   ├───unicase-355ca4fd327fabfd
        │   ├───unicase-5ce264aa1273838b
        │   ├───unicase-8ac0f082768d424a
        │   ├───unicase-cf024d918a538866
        │   ├───unicase-e9044cba12acdee6
        │   ├───unicode-bidi-bb475f0c3944d6b7
        │   ├───unicode-normalization-64e9089438d2ebfd
        │   ├───unicode-xid-a7467f62a39abf94
        │   ├───unreachable-67223564bef6342d
        │   ├───url-58e1116538252022
        │   ├───uuid-8d3ab2c31242ff75
        │   ├───version_check-8ad7f975e23d024d
        │   ├───void-0896fe1d47015a04
        │   ├───want-6f48436d34182abd
        │   ├───winapi-4a53e903331f64f4
        │   ├───winapi-4b6d1267534d970d
        │   ├───winapi-6f75c5e1021b74d7
        │   ├───winapi-752db0e8208ff517
        │   ├───winapi-build-2ed6cec7c9de885d
        │   ├───ws2_32-sys-5c26f11f3d3ba52f
        │   ├───ws2_32-sys-a6c2fe9a71ea1454
        │   └───ws2_32-sys-e4a5aabbb21dd0b8
        ├───build
        │   ├───arrayvec-646ad0ece92a7d00
        │   │   └───out
        │   ├───arrayvec-8dcfe3021b2d6a8c
        │   ├───backtrace-4a5d8bbe58343768
        │   │   └───out
        │   ├───backtrace-76389fa126a505ca
        │   ├───byteorder-6005d7caf2ed9454
        │   ├───byteorder-cacda78970cb326d
        │   │   └───out
        │   ├───crc32fast-601ce62d000e21bd
        │   │   └───out
        │   ├───crc32fast-9c59fdb01a618f87
        │   ├───encoding_rs-d0dc5ad1a07eed3c
        │   ├───encoding_rs-da47aa074a980d52
        │   │   └───out
        │   ├───httparse-3bfb6bbc4103b725
        │   ├───httparse-a633d1291de4b063
        │   │   └───out
        │   ├───kernel32-sys-323344e9ba717bff
        │   │   └───out
        │   ├───kernel32-sys-83eb403a9b94134f
        │   ├───libc-058cf61bc83378d2
        │   ├───libc-7939fbb070556365
        │   │   └───out
        │   ├───mime_guess-78f6fdcc75bfb801
        │   ├───mime_guess-b0cdb33e00444552
        │   │   └───out
        │   ├───native-tls-6abda051a7bbee28
        │   ├───native-tls-f608c79f4e18dacd
        │   │   └───out
        │   ├───num-integer-3b8af9e09499eb17
        │   │   └───out
        │   ├───num-integer-f5b2b5fbdf9ab12f
        │   ├───num-traits-122ab06899abc72a
        │   ├───num-traits-892c39b3d2eb30ab
        │   │   └───out
        │   ├───parking_lot_core-5118c128aa288759
        │   ├───parking_lot_core-ff708f491c5bff4c
        │   │   └───out
        │   ├───proc-macro2-3730dc9da7871eda
        │   │   └───out
        │   ├───proc-macro2-a7ad7b68ac88e1e9
        │   ├───rand-7bb4cfe73bf0dd1e
        │   ├───rand-e1919e05ce4b78ef
        │   │   └───out
        │   ├───rand_chacha-5b135890bc288e05
        │   │   └───out
        │   ├───rand_chacha-88fea0759acfb849
        │   ├───rand_pcg-04b94dbba308ed6e
        │   │   └───out
        │   ├───rand_pcg-ee7758f0896a3792
        │   ├───ryu-5f4d3c0db59c3c54
        │   │   └───out
        │   ├───ryu-6ba019180c4a274b
        │   ├───serde-d8d5485694af815f
        │   ├───serde-fe431a9eda84f305
        │   │   └───out
        │   ├───unicase-355ca4fd327fabfd
        │   │   └───out
        │   ├───unicase-5ce264aa1273838b
        │   ├───unicase-8ac0f082768d424a
        │   │   └───out
        │   ├───unicase-e9044cba12acdee6
        │   ├───winapi-6f75c5e1021b74d7
        │   │   └───out
        │   ├───winapi-752db0e8208ff517
        │   ├───ws2_32-sys-a6c2fe9a71ea1454
        │   │   └───out
        │   └───ws2_32-sys-e4a5aabbb21dd0b8
        ├───deps
        ├───examples
        ├───incremental
        │   ├───gitlab-18ycyqu9smunr
        │   │   └───s-f97zmznulc-1et4slk-glq89ki51e1z
        │   ├───gitlab-2ig8akjm0w1yx
        │   │   ├───s-f980cgaxw0-1jrgkvo-1ievukzcrjc88
        │   │   └───s-f981eiai94-z9lt25-working
        │   └───gitlab-3fwelxjyajmph
        │       └───s-f97zp4swxe-tfxev5-1duuud7rw47m5
        └───native

If I make my main.rs compile by following all available advice, then I break the library which I have not touched: 如果我遵循所有可用的建议使main.rs编译,那么我将破坏我没有碰过的库:

error[E0433]: failed to resolve: maybe a missing extern crate crates;? --> src\types.rs:17:5 | 17 | use crates::chrono::{DateTime, NaiveDate, Utc}; | ^^^^^^ maybe a missing extern crate crates;?

First things first, go back and re-read The Rust Programming Language 's chapter on "Packages, Crates, and Modules" . 首先,请返回并重新阅读Rust编程语言的“包装,板条箱和模块”一章 This discusses several fundamental concepts that are vital for understanding. 本文讨论了对理解至关重要的几个基本概念。

Why can I not clone a git project, add a main.rs and import one of the structs? 为什么我不能克隆git项目,添加main.rs并导入其中一个结构?

To me, this feels like the same question as "why can't I reach into another human, grab their lungs, and then use them to breathe"? 对我来说,这就像是一个问题:“为什么我不能伸入另一个人,抓住他们的肺,然后用它们呼吸”? You simply cannot download some arbitrary Rust library and start pulling random files out of it and expect them to work. 您根本无法下载一些任意的Rust库并开始从中提取随机文件并期望它们能正常工作。

Specifically in your case, all libraries have a lib.rs that is the crate root . 特别是在您的情况下,所有库都有一个lib.rs ,它是crate root The crate root tends to have many common definitions that are needed by the rest of the code and imports all the submodules. 板条箱根通常具有许多其余代码所需的通用定义,并导入所有子模块。 When you create a main.rs and declare the modules from the library as your own, your main.rs becomes the crate root, but it doesn't define all the things that the library needs. 当您创建一个main.rs并将库中的模块声明为您自己的模块时,您的main.rs 成为了板条箱根,但它并未定义库所需的所有内容。 This causes the code to fail to compile. 这导致代码无法编译。

Instead, just use the crate as a library , as it's intended. 相反,只需将板条箱用作库即可

The easiest thing to do create a new Cargo project and add the crate as a dependency to your Cargo.toml. 创建一个新的Cargo项目并将包装箱作为依赖项添加到您的Cargo.toml中是最简单的事情。 The gitlab crate is already distributed on crates.io , so you just add the version number: gitlab板条箱已经分配在crates.io上 ,因此您只需添加版本号即可:

[dependencies]
gitlab = "0.1104.1"

Add use gitlab::types::* in your main.rs and write your code. main.rs添加use gitlab::types::*并编写代码。


If you need to use the locally-checked out sources (perhaps you are modifying the crate?), or if you really want to edit the upstream library to have both a lib.rs and main.rs , see: 如果您需要使用本地检出的源(也许您正在修改板条箱?),或者您是否真的想编辑上游库以同时具有lib.rsmain.rs ,请参见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM