質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
データ構造

データ構造とは、データの集まりをコンピュータの中で効果的に扱うために、一定の形式に系統立てて格納する形式を指します。(配列/連想配列/木構造など)

Rust

Rustは、MoFoが支援するプログラミング言語。高速性を維持しつつも、メモリ管理を安全に行うことが可能な言語です。同じコンパイル言語であるC言語やC++では困難だったマルチスレッドを実装しやすく、並行性という点においても優れています。

Q&A

解決済

1回答

1153閲覧

rustの構造体を使ったプログラムの実装

takahashi1031

総合スコア7

データ構造

データ構造とは、データの集まりをコンピュータの中で効果的に扱うために、一定の形式に系統立てて格納する形式を指します。(配列/連想配列/木構造など)

Rust

Rustは、MoFoが支援するプログラミング言語。高速性を維持しつつも、メモリ管理を安全に行うことが可能な言語です。同じコンパイル言語であるC言語やC++では困難だったマルチスレッドを実装しやすく、並行性という点においても優れています。

0グッド

0クリップ

投稿2021/11/29 07:28

rustの構造体を使って以下のことを実装したい

set_enable関数はenabledをtrue(1)にする
set_direction関数はenabledがtrueではないとき処理を抜け、trueである場合directionをtrueに
set_output_statusはenabledがtrueではないかつdirectionもtrueではないとき処理を抜け
trueである場合 outputをtrueにする

rust

1struct Gpio{ 2 enabled:bool, 3 direction:bool, 4 output:bool, 5 mode:u32 6} 7 8impl Gpio{ 9 pub fn set_enable(&mut self){ 10 self.enabled=true; 11 } 12 13 pub fn set_direction(&mut self){ 14 if self.enabled=!true{ 15 Err(); 16 } 17 self.direction=true; 18 } 19 20 pub fn set_output_status(&mut self){ 21 if self.enabled=!true{ 22 Err(); 23 } 24 if self.direction=!true{ 25 Err(); 26 } 27 self.output=true; 28 } 29} 30 31fn main(){ 32 let mut gpio=Gpio{ 33 enabled:true, 34 direction:true, 35 output:true, 36 }; 37 gpio.set_enable(); 38 let pin=gpio.set_enable(); 39 println!("{}",pin); 40}

実行すると以下のようなエラーが出ますがどこをどう修正すればいいかわかりません
助言をお願いしたいです

rror[E0425]: cannot find value `enabled` in this scope --> src\main.rs:10:9 | 10 | enabled=true; | ^^^^^^^ help: you might have meant to use the available field: `self.enabled` error[E0425]: cannot find value `enabled` in this scope --> src\main.rs:14:12 | 14 | if enabled=!true{ | ^^^^^^^ | help: you might have meant to use pattern matching | 14 | if let enabled=!true{ | ^^^ help: you might have meant to use the available field | 14 | if self.enabled=!true{ | ^^^^^^^^^^^^ error[E0425]: cannot find value `direction` in this scope --> src\main.rs:17:9 | 17 | direction=true; | ^^^^^^^^^ help: you might have meant to use the available field: `self.direction` error[E0425]: cannot find value `enabled` in this scope --> src\main.rs:21:12 | 21 | if enabled=!true{ | ^^^^^^^ | help: you might have meant to use pattern matching | 21 | if let enabled=!true{ | ^^^ help: you might have meant to use the available field | 21 | if self.enabled=!true{ | ^^^^^^^^^^^^ error[E0425]: cannot find value `direction` in this scope --> src\main.rs:24:12 | 24 | if direction=!true{ | ^^^^^^^^^ | help: you might have meant to use pattern matching | 24 | if let direction=!true{ | ^^^ help: you might have meant to use the available field | 24 | if self.direction=!true{ | ^^^^^^^^^^^^^^ error[E0425]: cannot find value `output` in this scope --> src\main.rs:27:9 | 27 | output=true; | ^^^^^^ help: you might have meant to use the available field: `self.output` error[E0061]: this function takes 1 argument but 0 arguments were supplied --> src\main.rs:15:13 | 15 | Err(); | ^^^-- supplied 0 arguments | | | expected 1 argument error[E0061]: this function takes 1 argument but 0 arguments were supplied --> src\main.rs:22:13 | 22 | Err(); | ^^^-- supplied 0 arguments | | | expected 1 argument error[E0061]: this function takes 1 argument but 0 arguments were supplied --> src\main.rs:25:13 | 25 | Err(); | ^^^-- supplied 0 arguments | | | expected 1 argument error[E0063]: missing field `mode` in initializer of `Gpio` --> src\main.rs:32:18 | 32 | let mut gpio=Gpio{ | ^^^^ missing `mode` error[E0277]: `()` doesn't implement `std::fmt::Display` --> src\main.rs:39:19 | 39 | println!("{}",pin); | ^^^ `()` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `()` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: required by `std::fmt::Display::fmt` = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 11 previous errors Some errors have detailed explanations: E0061, E0063, E0277, E0425. For more information about an error, try `rustc --explain E0061`. error: could not compile `test_cargo` To learn more, run the command again with --verbose.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ozwk

2021/11/29 07:38

過去の質問が4つほど放置されています。処理してください。
takahashi1031

2021/11/29 07:59

Err()で処理を抜ける方法がわかりません。Result型というのにしなくてはいけないようですが breakでifを抜けれるかと思いやってみるとエラーが出てしまいます。 ここも助言いただければ幸いです
guest

回答1

0

ベストアンサー

=!ではなく!=です。
Err()は引数が1つ必要です。
let mut gpio=Gpio{...で、フィールド:modeの初期化がされてません。
let pin=gpio.set_enable();とありますが、これは何がしたいのでしょう? set_enable()は何も返しません。


set_direction関数はenabledがtrueではないとき処理を抜け、trueである場合directionをtrueに

この、「処理を抜け」るが本当にただ何もせずに処理を抜けるだけならreturn;と書きましょう。

投稿2021/11/29 07:35

編集2021/11/29 08:03
ozwk

総合スコア13521

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問