両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン |
rust:language_memo [2024/06/14 05:17] – [できないと言われてもそうしたい時があります...😅] ともやん | rust:language_memo [2024/06/17 07:09] (現在) – [もしもハマってしまったら...🔰] ともやん |
---|
</WRAP> | </WRAP> |
| |
===== 慣れていないと戸惑うもの😲 ===== | ===== 慣れていないと戸惑うもの...😲 ===== |
[[https://doc.rust-jp.rs/rust-by-example-ja/error/option_unwrap.html|Option と unwrap - Rust By Example 日本語版]]\\ | [[https://doc.rust-jp.rs/rust-by-example-ja/error/option_unwrap.html|Option と unwrap - Rust By Example 日本語版]]\\ |
[[https://doc.rust-jp.rs/rust-by-example-ja/error/option_unwrap/question_mark.html|?によるOptionのアンパック - Rust By Example 日本語版]]\\ | [[https://doc.rust-jp.rs/rust-by-example-ja/error/option_unwrap/question_mark.html|?によるOptionのアンパック - Rust By Example 日本語版]]\\ |
[[https://doc.rust-jp.rs/rust-by-example-ja/error/result.html|Result - Rust By Example 日本語版]]\\ | [[https://doc.rust-jp.rs/rust-by-example-ja/error/result.html|Result - Rust By Example 日本語版]]\\ |
| |
===== できないと言われてもそうしたい時があります...😅 ===== | ==== いちいち細々とうるさいコンパイラを黙らせる😝 ==== |
| <WRAP color_term> |
| <WRAP color_result><html><pre> |
| <font color="#A2734C"><b>warning</b></font><b>: unused imports: `Spinner`, `Spinners`</b> |
| <font color="#2A7BDE"><b>--> </b></font>src/main.rs:24:16 |
| <font color="#2A7BDE"><b>|</b></font> |
| <font color="#2A7BDE"><b>24</b></font> <font color="#2A7BDE"><b>|</b></font> use spinners::{Spinner, Spinners}; |
| <font color="#2A7BDE"><b>| </b></font> <font color="#A2734C"><b>^^^^^^^</b></font> <font color="#A2734C"><b>^^^^^^^^</b></font> |
| <font color="#2A7BDE"><b>|</b></font> |
| <font color="#2A7BDE"><b>= </b></font><b>note</b>: `#[warn(unused_imports)]` on by default |
| </pre></html></WRAP> |
| </WRAP> |
| |
| <html><code>#[warn(unused_imports)]</code></html> を <html><code>#[allow(unused_imports)]</code></html> にして、該当箇所に貼り付けて黙らせる😝\\ |
| <wrap em>あとから **allow** を検索して修正しないと直し漏れる可能性がある🤔</wrap>\\ |
| <WRAP color_term> |
| <WRAP color_mincode><html><pre> |
| <font color="#444444"> 23</font> <font color="#F8F8F2">#[allow(unused_imports)]</font> |
| <font color="#444444"> 24</font> <font color="#F92672">use</font><font color="#F8F8F2"> spinners::{Spinner, Spinners};</font> |
| </pre></html></WRAP> |
| </WRAP> |
| |
| === モジュール、クレート全体に属性を付与 === |
| [[https://doc.rust-lang.org/reference/attributes.html|Attributes - The Rust Reference]]\\ |
| [[so>questions/27454761/what-is-a-crate-attribute-and-where-do-i-add-it|rust - What is a crate attribute and where do I add it? - Stack Overflow]]\\ |
| |
| ファイルの先頭で <html><code>#!</code></html> 構文で <html><code>#![allow(unused)]</code></html>を使う🤔\\ |
| diesel_ext で model を生成すると書かれている😉\\ |
| <WRAP color_term> |
| <WRAP color_result><html><pre> |
| <font color="#75715E">// Generated by diesel_ext</font> |
| |
| <font color="#F8F8F2">#![allow(unused)]</font> |
| </pre></html></WRAP> |
| </WRAP> |
| ===== できないと言われてもしたくなる時があります...😅 ===== |
**String を &'static str に変換したい**\\ | **String を &'static str に変換したい**\\ |
String はプログラムの存続期間全体に渡って存続していないので、String から &'static str を取得することはできない...🤔\\ | String はプログラムの存続期間全体に渡って存続していないので、String から &'static str を取得することはできない...🤔\\ |
[[so>questions/23975391/how-to-convert-a-string-into-a-static-str|rust - How to convert a String into a &'static str - Stack Overflow]]\\ | [[so>questions/23975391/how-to-convert-a-string-into-a-static-str|rust - How to convert a String into a &'static str - Stack Overflow]]\\ |
| |
===== ふつうのプログラミング言語にはありそうで Rust には無いもの😳 ===== | ===== Rust にふつうにありそうで無いもの...😳 ===== |
| |
==== Try - Catch ステートメント ==== | ==== Try - Catch ステートメント ==== |
[[so>questions/55755552/what-is-the-rust-equivalent-to-a-try-catch-statement|error handling - What Is the Rust Equivalent to a Try-Catch Statement? - Stack Overflow]]\\ | [[so>questions/55755552/what-is-the-rust-equivalent-to-a-try-catch-statement|error handling - What Is the Rust Equivalent to a Try-Catch Statement? - Stack Overflow]]\\ |
| |
| ===== もしもハマってしまったら...🤪🔰 ===== |
| |
| ==== cargo add tokio してるのに #[tokio::main] が使えない 😅 ==== |
| [[so>questions/63874178/cannot-find-tokiomain-macro|rust - Cannot find tokio::main macro? - Stack Overflow]]\\ |
| <WRAP color_term> |
| <WRAP color_result><html><pre> |
| <font color="#F66151"><b>error[E0433]</b></font><b>: failed to resolve: could not find `main` in `tokio`</b> |
| <font color="#2A7BDE"><b>--> </b></font>src/main.rs:69:10 |
| <font color="#2A7BDE"><b>|</b></font> |
| <font color="#2A7BDE"><b>69</b></font> <font color="#2A7BDE"><b>|</b></font> #[tokio::main] |
| <font color="#2A7BDE"><b>| </b></font> <font color="#F66151"><b>^^^^</b></font> <font color="#F66151"><b>could not find `main` in `tokio`</b></font> |
| </pre></html></WRAP> |
| </WRAP> |
| |
| [[https://docs.rs/tokio/latest/tokio/attr.main.html|main in tokio - Rust]]\\ |
| <html><code>#[main]</code></html> には Cargo.toml の <html><code>features = ["rt", "macros"]</code></html> が必要です🤔\\ |
| Available on crate features <wrap em>rt</wrap> and <wrap em>macros</wrap> only.\\ |
| <WRAP color_term> |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269">bat</font><font color="#FF9999"> </font><font color="#A347BA">-p</font><font color="#FF9999"> </font><font color="#A347BA">--pager=never</font><font color="#FF9999"> </font><font color="#FF9999"><u style="text-decoration-style:solid">Cargo.toml</u></font> |
| </pre></html></WRAP> |
| <WRAP color_result><html><pre> |
| <font color="#F92672">tokio</font><font color="#F8F8F2"> = { </font><font color="#F92672">version</font><font color="#F8F8F2"> = </font><font color="#E6DB74">"1.38.0"</font><font color="#F8F8F2">, </font><font color="#F92672">features</font><font color="#F8F8F2"> = [</font><font color="#E6DB74">"rt"</font><font color="#F8F8F2">, </font><font color="#E6DB74">"macros"</font><font color="#F8F8F2">] }</font> |
| </pre></html></WRAP> |
| </WRAP> |
| |
| <html><code>#[tokio::main]</code></html> には Cargo.toml の <html><code>features = ["rt-multi-thread", "macros"]</code></html> が必要です🤔\\ |
| Note: The multi-threaded runtime requires the <wrap em>rt-multi-thread</wrap> feature flag.\\ |
| <WRAP color_term> |
| <WRAP color_command><html><pre> |
| <font color="#0087FF"><b>$</b></font> <font color="#26A269">bat</font><font color="#FF9999"> </font><font color="#A347BA">-p</font><font color="#FF9999"> </font><font color="#A347BA">--pager=never</font><font color="#FF9999"> </font><font color="#FF9999"><u style="text-decoration-style:solid">Cargo.toml</u></font> |
| </pre></html></WRAP> |
| <WRAP color_result><html><pre> |
| <font color="#F92672">tokio</font><font color="#F8F8F2"> = { </font><font color="#F92672">version</font><font color="#F8F8F2"> = </font><font color="#E6DB74">"1.38.0"</font><font color="#F8F8F2">, </font><font color="#F92672">features</font><font color="#F8F8F2"> = [</font><font color="#E6DB74">"rt-multi-thread"</font><font color="#F8F8F2">, </font><font color="#E6DB74">"macros"</font><font color="#F8F8F2">] }</font> |
| </pre></html></WRAP> |
| </WRAP> |
| |
===== FFI - Foreign Function Interface ===== | ===== FFI - Foreign Function Interface ===== |