次のコードに問題があります。
trait HelloPhrase {
fn hello(&self, to: &'static str);
}
pub enum GetHelloResult<H: HelloPhrase> {
Matched(H),
NoMatch,
}
struct English;
impl English {
pub fn new() -> English {
English
}
}
impl HelloPhrase for English {
fn hello(&self, to: &'static str) {
println!("Hello {}.", to)
}
}
struct Phrases<H: HelloPhrase> {
hello_phrases: std::collections::HashMap<&'static str, H>,
}
impl<H: HelloPhrase> Phrases<H> {
pub fn new() -> Phrases<H> {
Phrases { hello_phrases: std::collections::HashMap::new() }
}
pub fn add_hello_phrase(&mut self, lang: &'static str, hello_phrase: H) {
self.hello_phrases.insert(lang, hello_phrase);
}
pub fn get_hello(&self, lang: &'static str) -> GetHelloResult<H> {
match self.hello_phrases.get(lang) {
Some(hello_phrase) => return GetHelloResult::Matched(hello_phrase),
_ => return GetHelloResult::NoMatch,
};
}
}
fn main() {
let mut hs = Phrases::new();
hs.add_hello_phrase("english", English::new());
match hs.get_hello("english") {
GetHelloResult::Matched(hello_phrase) => hello_phrase.hello("Tom"),
_ => println!("HelloPhrase not found"),
}
}
(再生リンク)
HelloPhrase
実装する言語、英語、ロシア語などの特性です。Phrases
マネージャー構造体であり、言語からフレーズへの多くのマップを持つことができます。これは不自然な例ですが、これをイベント マネージャー (つまり、X 入力のイベント ハンドラーを取得する)、または HTTP ハンドラーおよびルーターと考えることができます。
そうは言っても、 a の所有権を借りHelloPhrase
て呼び出し元に返す方法を理解するのに苦労しています。実行すると、次のエラーが返されます。
<anon>:40:66: 40:78 error: mismatched types:
expected `H`,
found `&H`
(expected type parameter,
found &-ptr) [E0308]
<anon>:40 Some(hello_phrase) => return GetHelloResult::Matched(hello_phrase),
^~~~~~~~~~~~
私は追加しようとしました:
pub fn get_hello(&self, lang: &'static str) -> GetHelloResult<&H> {
と
pub enum GetHelloResult<H: HelloPhrase> {
Matched(&H),
NoMatch,
}
(再生リンク)
次のエラーが発生します。
<anon>:7:13: 7:15 error: missing lifetime specifier [E0106]
<anon>:7 Matched(&H),
列挙型にライフタイムを追加するのに問題があります-理論的には、戻り値のライフタイムを構造体のライフタイムにしたいのですPhrases
が、ライフタイムの構文はこれまでのところ非常に混乱しています。これを 2 つの質問にまとめると、次のようになります。
GetHelloResult
このエラーを満たすために、 にライフタイムを追加するにはどうすればよいですか?- Rust の所有規則に基づいて、Rust でアンチパターンを実行しようとしていますか? このようなものには、どのようなデザインが適しているでしょうか?
ドキュメントに基づいて、構造体でライフタイムを使用する方法は知っていますが、列挙型にライフタイムを追加する方法はわかりません (構文に関して)。構造体の有効期間についてだけ言及したのは、それが欠けている部分だと思うからですが、正直なところわかりません。hello_phrases
さらに、構造体と実装にライフタイムを追加してマップに追加しようとすると、エラーが発生します
the parameter type `H` may not live long enough [E0309]