Skip to content

Instantly share code, notes, and snippets.

@dhilst
Created August 21, 2024 16:06
Show Gist options
  • Save dhilst/922ed82f409a94883202b47128f114a5 to your computer and use it in GitHub Desktop.
Save dhilst/922ed82f409a94883202b47128f114a5 to your computer and use it in GitHub Desktop.
#![feature(try_blocks, try_trait_v2)]
#![allow(dead_code)]
#[derive(Debug)]
struct Foo<T>(T);
// Parametric types are bound during method calls
trait Bar<T> {
// Associated types are bound during Trait implementation
type AT;
fn associated_type(&self) -> Self::AT {
todo!()
}
fn parametric_type(&self) -> T {
todo!()
}
}
impl Bar<String> for Foo<String> {
type AT = bool;
fn associated_type(&self) -> Self::AT {
std::todo!()
}
fn parametric_type(&self) -> String {
std::todo!()
}
}
impl Bar<i32> for Foo<i32> {
type AT = &'static str;
fn associated_type(&self) -> Self::AT {
std::todo!()
}
fn parametric_type(&self) -> i32 {
std::todo!()
}
}
fn main() {
let foos = Foo(String::from("foo"));
let fooi = Foo(0);
let _x: bool = Bar::<String>::associated_type(&foos);
let _x: &str = Bar::<i32>::associated_type(&fooi);
let _x: String = Bar::<String>::parametric_type(&foos);
let _x: i32 = Bar::<i32>::parametric_type(&fooi);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment