Skip to content

Instantly share code, notes, and snippets.

@Latrasis
Last active March 6, 2017 18:05
Show Gist options
  • Save Latrasis/a55170b4014b907c6fed93f86b2a5427 to your computer and use it in GitHub Desktop.
Save Latrasis/a55170b4014b907c6fed93f86b2a5427 to your computer and use it in GitHub Desktop.
Newtype Support Concept
// Type Alias Definition
type CharVec = Vec<char>;
// Impl Trait for Alias
impl ToString for CharVec {
fn to_string(&self) -> String {
String::from_iter(self)
}
}
fn foo<T: ToString>(bar: T) {
println!("My String {:}", bar.to_string());
}
fn main() {
let sample = vec!['h','e','l','l','o'];
// Compiles
foo(sample as CharVec);
// Does not Compile
foo(sample);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment