Rust ランゲージ メモ

$ cargo new haruo_axum

    Creating binary (application) `haruo_axum` package
note: see more `Cargo.toml` keys and their definitions at

$ tree haruo_axum

├── Cargo.toml
└── src

Cargo.toml を手で編集することなく、cargo add で好きなクレートをどんどん追加して開発できる😊

$ cd haruo_axum
$ bat -p Cargo.toml

name = "haruo_axum"
version = "0.1.0"
edition = "2021"


Axum を追加する場合…🤔

$ cargo add axum

    Updating index
      Adding axum v0.7.5 to dependencies
             + form
             + http1
             + json
             + matched-path
             + original-uri
             + query
             + tokio
             + tower-log
             + tracing
             - __private_docs
             - http2
             - macros
             - multipart
             - ws
    Updating index

$ bat -p Cargo.toml

name = "haruo_axum"
version = "0.1.0"
edition = "2021"

axum = "0.7.5"

Features (特徴) を有効にしてクレートを追加する場合…🤔

$ cargo add tokio --features rt-multi-thread


$ cargo add tokio -F rt-multi-thread

    Updating index
      Adding tokio v1.38.0 to dependencies
             + 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 index
      Adding diesel v2.2.0 to dependencies
             + 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 index

$ cargo add diesel -F postgres,r2d2,chrono,i-implement-a-third-party-backend-and-opt-into-breaking-changes

    Updating index
      Adding diesel v2.2.0 to dependencies
             + 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 index
      Adding assert_cmd v2.0.14 to dev-dependencies
             - color
             - color-auto
      Adding predicates v3.1.0 to dev-dependencies
             + color
             + diff
             + float-cmp
             + normalize-line-endings
             + regex
             - unstable
    Updating index


$ cargo add serde -F derive
$ cargo add tracing-subscriber
$ cargo add clap -F derive


$ 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

warning: unused imports: `Spinner`, `Spinners`
  --> src/
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


String を &'static str に変換したい
String はプログラムの存続期間全体に渡って存続していないので、String から &'static str を取得することはできない…🤔
rust - How to convert a String into a &'static str - Stack Overflow

rust - Cannot find tokio::main macro? - Stack Overflow

error[E0433]: failed to resolve: could not find `main` in `tokio`
  --> src/
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"] }

  • rust/language_memo.txt
  • 最終更新: 2024/06/17 07:09
  • by ともやん