目次
Rust ランゲージ メモ
よく使う機能 - Cargo 編 😍
新しいパッケージの作成 [new]
$ cargo new haruo_axum
Creating binary (application) `haruo_axum` package note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
$ tree haruo_axum
haruo_axum ├── Cargo.toml └── src └── main.rs
依存クレートの追加 [add]
Cargo.toml を手で編集することなく、cargo add で好きなクレートをどんどん追加して開発できる😊
$ cd haruo_axum $ bat -p Cargo.toml
[package] name = "haruo_axum" version = "0.1.0" edition = "2021" [dependencies]
Axum を追加する場合…🤔
$ cargo add axum
Updating crates.io index Adding axum v0.7.5 to dependencies Features: + form + http1 + json + matched-path + original-uri + query + tokio + tower-log + tracing - __private_docs - http2 - macros - multipart - ws Updating crates.io index
$ bat -p Cargo.toml
[package] name = "haruo_axum" version = "0.1.0" edition = "2021" [dependencies] axum = "0.7.5"
Features (特徴) を有効にしてクレートを追加する場合…🤔
$ cargo add tokio --features rt-multi-thread
Or
$ cargo add tokio -F rt-multi-thread
Updating crates.io index Adding tokio v1.38.0 to dependencies Features: + num_cpus + rt + rt-multi-thread - bytes - fs - full - io-std - io-util - libc - macros - mio - net - parking_lot - process - signal - signal-hook-registry - socket2 - sync - test-util - time - tokio-macros - tracing - windows-sys
依存クレートを追加済みでも、Features をあとから追加で有効化できる…🤔
Features を複数有効化したい場合はカンマ(“,”) で区切って指定する😊
$ cargo add diesel
Updating crates.io index Adding diesel v2.2.0 to dependencies Features: + 32-column-tables + with-deprecated - 128-column-tables - 64-column-tables - __with_asan_tests - chrono - extras - huge-tables - i-implement-a-third-party-backend-and-opt-into-breaking-changes - ipnet-address - large-tables - mysql - mysql_backend - mysqlclient-src - network-address - numeric - postgres - postgres_backend - pq-src - quickcheck - r2d2 - returning_clauses_for_sqlite_3_35 - serde_json - sqlite - time - unstable - uuid - without-deprecated Updating crates.io index
$ cargo add diesel -F postgres,r2d2,chrono,i-implement-a-third-party-backend-and-opt-into-breaking-changes
Updating crates.io index Adding diesel v2.2.0 to dependencies Features: + 32-column-tables + chrono + i-implement-a-third-party-backend-and-opt-into-breaking-changes + postgres + postgres_backend + r2d2 + with-deprecated - 128-column-tables - 64-column-tables - __with_asan_tests - extras - huge-tables - ipnet-address - large-tables - mysql - mysql_backend - mysqlclient-src - network-address - numeric - pq-src - quickcheck - returning_clauses_for_sqlite_3_35 - serde_json - sqlite - time - unstable - uuid - without-deprecated
開発時のみ必要なクレートは、--dev
オプションを指定して追加する🤔
$ cargo add --dev assert_cmd predicates
Updating crates.io index Adding assert_cmd v2.0.14 to dev-dependencies Features: - color - color-auto Adding predicates v3.1.0 to dev-dependencies Features: + color + diff + float-cmp + normalize-line-endings + regex - unstable Updating crates.io index
必要なクレートをどんどん追加する😅
$ cargo add serde -F derive $ cargo add tracing-subscriber $ cargo add clap -F derive
コードチェック [check]
パッケージコードと依存関係にエラーがないか確認する🤔
$ cargo check
Downloaded diesel_table_macro_syntax v0.2.0
Downloaded utf8parse v0.2.2
Downloaded dsl_auto_type v0.1.0
Downloaded httparse v1.9.3
Downloaded parking_lot v0.12.3
Downloaded clap v4.5.7
Downloaded diesel_derives v2.2.0
Downloaded clap_builder v4.5.7
Downloaded diesel v2.2.0
Downloaded 9 crates (759.2 KB) in 2m 56s
Compiling proc-macro2 v1.0.85
Compiling unicode-ident v1.0.12
Compiling libc v0.2.155
Checking itoa v1.0.11
Compiling autocfg v1.3.0
Checking pin-project-lite v0.2.14
Checking smallvec v1.13.2
Checking futures-core v0.3.30
Checking log v0.4.21
Checking once_cell v1.19.0
Compiling rustversion v1.0.17
Checking fnv v1.0.7
Checking pin-utils v0.1.0
Checking cfg-if v1.0.0
Checking futures-task v0.3.30
Checking bytes v1.6.0
Checking http v1.1.0
Compiling quote v1.0.36
Compiling syn v2.0.66
Checking futures-util v0.3.30
Checking tracing-core v0.1.32
Compiling lock_api v0.4.12
Compiling serde v1.0.203
Compiling strsim v0.11.1
Compiling ident_case v1.0.1
Compiling parking_lot_core v0.9.10
Checking http-body v1.0.0
Checking mio v0.8.11
Checking socket2 v0.5.7
Checking num_cpus v1.16.0
Compiling heck v0.5.0
Compiling httparse v1.9.3
Checking scopeguard v1.2.0
Compiling num-traits v0.2.19
Checking utf8parse v0.2.2
Checking parking_lot v0.12.3
Checking anstyle-parse v0.2.4
Checking tracing v0.1.40
Compiling axum-core v0.4.3
Checking futures-channel v0.3.30
Checking percent-encoding v2.3.1
Checking httpdate v1.0.3
Checking tower-service v0.3.2
Checking tower-layer v0.3.2
Checking colorchoice v1.0.1
Compiling either v1.12.0
Checking ryu v1.0.18
Checking anstyle v1.0.7
Checking anstyle-query v1.1.0
Checking is_terminal_polyfill v1.70.0
Compiling pq-sys v0.6.0
Compiling serde_json v1.0.117
Checking anstream v0.6.14
Checking form_urlencoded v1.2.1
Checking scheduled-thread-pool v0.2.7
Checking http-body-util v0.1.2
Compiling axum v0.7.5
Checking mime v0.3.17
Checking overload v0.1.1
Checking clap_lex v0.7.1
Checking sync_wrapper v0.1.2
Compiling darling_core v0.20.9
Compiling diesel_table_macro_syntax v0.2.0
Checking lazy_static v1.4.0
Checking iana-time-zone v0.1.60
Checking sharded-slab v0.1.7
Checking chrono v0.4.38
Checking clap_builder v4.5.7
Checking nu-ansi-term v0.46.0
Checking r2d2 v0.8.10
Checking tracing-log v0.2.0
Checking thread_local v1.1.8
Checking sync_wrapper v1.0.1
Checking memchr v2.7.2
Checking matchit v0.7.3
Checking bitflags v2.5.0
Checking byteorder v1.5.0
Checking tracing-subscriber v0.3.18
Compiling serde_derive v1.0.203
Compiling tokio-macros v2.3.0
Compiling darling_macro v0.20.9
Compiling pin-project-internal v1.1.5
Checking tokio v1.38.0
Compiling darling v0.20.9
Compiling dsl_auto_type v0.1.0
Compiling async-trait v0.1.80
Checking pin-project v1.1.5
Compiling diesel_derives v2.2.0
Checking hyper v1.3.1
Checking hyper-util v0.1.5
Checking tower v0.4.13
Compiling clap_derive v4.5.5
Checking serde_urlencoded v0.7.1
Checking serde_path_to_error v0.1.16
Checking diesel v2.2.0
Checking clap v4.5.7
Checking haruo_axum v0.1.0 (/home/tomoyan/my_projects/haruo_axum)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 3m 57s
慣れていないと戸惑うもの...😲
Option と unwrap - Rust By Example 日本語版
?によるOptionのアンパック - Rust By Example 日本語版
Result - Rust By Example 日本語版
いちいち細々とうるさいコンパイラを黙らせる😝
warning: unused imports: `Spinner`, `Spinners` --> src/main.rs:24:16 | 24 | use spinners::{Spinner, Spinners}; | ^^^^^^^ ^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
#[warn(unused_imports)]
を #[allow(unused_imports)]
にして、該当箇所に貼り付けて黙らせる😝
あとから allow を検索して修正しないと直し漏れる可能性がある🤔
23 #[allow(unused_imports)] 24 use spinners::{Spinner, Spinners};
モジュール、クレート全体に属性を付与
Attributes - The Rust Reference
rust - What is a crate attribute and where do I add it? - Stack Overflow
ファイルの先頭で #!
構文で #![allow(unused)]
を使う🤔
diesel_ext で model を生成すると書かれている😉
// Generated by diesel_ext #![allow(unused)]
できないと言われてもしたくなる時があります...😅
String を &'static str に変換したい
String はプログラムの存続期間全体に渡って存続していないので、String から &'static str を取得することはできない…🤔
rust - How to convert a String into a &'static str - Stack Overflow
Rust にふつうにありそうで無いもの...😳
Try - Catch ステートメント
もしもハマってしまったら...🤪🔰
cargo add tokio してるのに #[tokio::main] が使えない 😅
rust - Cannot find tokio::main macro? - Stack Overflow
error[E0433]: failed to resolve: could not find `main` in `tokio`
--> src/main.rs:69:10
|
69 | #[tokio::main]
| ^^^^ could not find `main` in `tokio`
main in tokio - Rust
#[main]
には Cargo.toml の features = ["rt", "macros"]
が必要です🤔
Available on crate features rt and macros only.
$ bat -p --pager=never Cargo.toml
tokio = { version = "1.38.0", features = ["rt", "macros"] }
#[tokio::main]
には Cargo.toml の features = ["rt-multi-thread", "macros"]
が必要です🤔
Note: The multi-threaded runtime requires the rt-multi-thread feature flag.
$ bat -p --pager=never Cargo.toml
tokio = { version = "1.38.0", features = ["rt-multi-thread", "macros"] }