Rust

Rust

Rustでstruct - mapの相互変換をするための derive macroを実装した

structとmapの相互変換 任意のstructをHashMapに変換する必要があったのでderive macroで実装しました。 できたものは以下になります。 GitHub - yutakahashi114/rust-map-macroContribute to yutakahashi114/rust-map-macro development by creating an account on GitHub. GitHubyutakahashi114 [https://github.com/yutakahashi114/rust-map-macro] Rustにはreflectionがない Rustは構造体からmap(HashMap)へ変換するための機能がないらしいです。 例えばGolangでは以下のように reflect パッケージを使うと実装できます。 Go言語でstructからmapへ変換 - Qiita構造体のフィールド一覧を取得したくなったので、reflectを使って実装。 struct2map.gopackage main import ( “fmt” “re
Yu Takahashi
Yu Takahashi
11 min read
Rust

Rustでベクターの要素から指定したindexの値を取得しようとしてハマった話

cannot move out of a shared reference Rust初心者です。上記のエラーでハマりました。 vectorの要素のうち指定したindexの値だけを取り出したい Goでいうと以下のような処理です。 func get() Hoge { vector := []Hoge{{name: "first"}} return vector[0] } type Hoge struct { name string } まあ普通にできます。Rustでこれと似たようなことをするとき、最初に以下のようなコード書きました。 fn get() -> Result<Hoge, String> { let vector: Vec<Hoge> = vec![Hoge { name: String::from("first"), }]; if let Some(&first) = vector.get(0) { return Ok(first);
Yu Takahashi
Yu Takahashi
2 min read