Skip to content

Instantly share code, notes, and snippets.

@func-hs
Created February 15, 2018 15:15
Show Gist options
  • Save func-hs/87fe4415cd7162d14f30559857a5be0f to your computer and use it in GitHub Desktop.
Save func-hs/87fe4415cd7162d14f30559857a5be0f to your computer and use it in GitHub Desktop.
型引数を返り値やメンバの型に使いまわせない奴
$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
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();
}
@func-hs
Copy link
Author

func-hs commented Feb 15, 2018

実体を関数(メソッド)内でダイレクトに作って渡そうとすると、型引数を無視した扱いになる模様(解決)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment