trait TMRecord {}
#[derive(Debug)]
struct A {
name: String,
}
impl A {
pub fn new(s: &str) -> Self {
Self {
name: s.to_string(),
}
}
}
#[derive(Debug)]
struct B {
name: String,
}
impl B {
pub fn new(s: &str) -> Self {
Self {
name: s.to_string(),
}
}
}
#[derive(Debug)]
pub struct Record<T> {
pub r: T,
}
impl<T> Record<T> {
pub fn new(s: T) -> Record<T> {
Record { r: s }
}
}
impl TMRecord for A {}
impl TMRecord for B {}
fn parse(s: &str, k: &str) -> Option<Record<T>> {
match k {
"byName" => Some(Record::<A>::new(A::new(s))),
"byNbr" => Some(Record::<B>::new(B::new(s))),
_ => None,
}
}
fn main() {
let s = "Some string key";
let res = parse(&s, &"byName");
let res = parse(&s, &"byNbr");
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0412]: cannot find type `T` in this scope
--> src/main.rs:43:45
|
4 | struct A {
| -------- similarly named struct `A` defined here
...
43 | fn parse(s: &str, k: &str) -> Option<Record<T>> {
| ^
|
help: a struct with a similar name exists
|
43 | fn parse(s: &str, k: &str) -> Option<Record<A>> {
| ~
help: you might be missing a type parameter
|
43 | fn parse<T>(s: &str, k: &str) -> Option<Record<T>> {
| +++
For more information about this error, try `rustc --explain E0412`.
error: could not compile `playground` due to previous error