Skip to content

Rust

Rust学習

The Rust Programming Language 日本語版 - The Rust Programming Language 日本語版、途中飛ばして12章やってたけれど、やっぱり最初からやったほうが良い気がしてきた。

そして、最初のページに

すべてのプロジェクトの Cargo.toml に edition=“2018” とあることを前提にしています。 との注意書きがあるのに今更ながら気づいた。

昨日エラーが出ると言っていたのは、edition = "2021"だったので、試しに2018にしてみたらエラーではなく警告に変わった。

Read more

dynとは?

リファクタリングしてモジュール性とエラー処理を向上させる - The Rust Programming Language 日本語版 を写経していて、コンパイルエラーに遭遇した。

$ cargo run
   Compiling mdmml_rust v0.1.0 (mdmml_rust)
error[E0782]: trait objects must include the `dyn` keyword
  --> src/main.rs:21:41
   |
21 | fn run(config: Config)-> Result<(), Box<Error>> {
   |                                         ^^^^^
   |
help: add `dyn` keyword before this trait
   |
21 - fn run(config: Config)-> Result<(), Box<Error>> {
21 + fn run(config: Config)-> Result<(), Box<dyn Error>> {
   | 

For more information about this error, try `rustc --explain E0782`.
error: could not compile `mdmml_rust` due to previous error

修正方法も提示してくれて親切。

で、このdynとは何なのか。書かれているようにrustc --explain E0782を実行すると、以下の出力となった。

Read more

VS CodeのRust用設定

コードフォーマッターが欲しいと思って、プラグインを入れたけれど動かない。

Couldn't start client Rust Language Server
---
Rustup not available. Install from https://www.rustup.rs/

どうやらツールが足りないようなので、追加でインストールした。

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source $HOME/.cargo/env
$ rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.59.0 (9d1b2106e 2022-02-23)`

それでもエラーが解消しなかったので、OSごと再起動したら反映された。

Read more

Rust再入門

以前RustでHello Worldするのはやってたので、再入門。

The Rust Programming Language 日本語版 - The Rust Programming Language 日本語版を見ながら。

今回はcargoを使ってビルドするようにしてみた。

マニュアルだとcargo newで作成しているが、先にGitHubでリポジトリを作ってしまっていたので、cargo initを使っている。

$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
$ source $HOME/.cargo/env
$ rustc --version
rustc 1.58.1 (db9d1b20b 2022-01-20)
$ cargo --version
cargo 1.58.0 (f01b232bc 2022-01-19)
$ cargo init
$ cargo build
   Compiling mdmml_rust v0.1.0 (/home/umemak/workspace/mdmml_rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.36s
$ ./target/debug/mdmml_rust 
Hello, world!
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/mdmml_rust`
Hello, world!

Rust入門

なぜDiscordはGoからRustへ移行するのか - MISONLN41’s Blog を読んで興味がわいたのでインストールしてみた。

Rust をインストール - Rustプログラミング言語 のWSLのコマンドで。インストール完了後、WSLログインしなおさないとrustcコマンド使えなかった。

$ rustc --version
rustc 1.45.1 (c367798cf 2020-07-26)

ファイル作って

$ vim hello.rs
fn main() {
    println!("Hello, world!");
}

コンパイル&実行

$ rustc hello.rs
$ ./hello
Hello, world!

できた。