Created
February 15, 2018 15:15
-
-
Save func-hs/87fe4415cd7162d14f30559857a5be0f to your computer and use it in GitHub Desktop.
型引数を返り値やメンバの型に使いまわせない奴
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$rustc --version | |
rustc 1.23.0 (766bd11c8 2018-01-01) | |
$ rustc test.rs | |
... | |
error[E0308]: mismatched types | |
--> test.rs:12:31 | |
| | |
12 | self.m.insert("hoge", S2); | |
| ^^ expected type parameter, found struct `S2` | |
| | |
= note: expected type `T` | |
found type `S2` | |
error[E0308]: mismatched types | |
--> test.rs:15:9 | |
| | |
14 | fn s2() -> T { | |
| - expected `T` because of return type | |
15 | S2 | |
| ^^ expected type parameter, found struct `S2` | |
| | |
= note: expected type `T` | |
found type `S2` | |
error: aborting due to 2 previous errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
trait Trait { | |
} | |
struct S1<T: Trait> { | |
m: HashMap<&'static str, T> | |
} | |
impl<T: Trait> S1<T> { | |
fn foo(&mut self) { | |
self.m.insert("hoge", S2); | |
} | |
} | |
struct S2; | |
impl Trait for S2 {} | |
fn main() { | |
let mut s: S1<S2> = S1 {m: HashMap::new()}; | |
s.foo(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実体を関数(メソッド)内でダイレクトに作って渡そうとすると、型引数を無視した扱いになる模様(解決)