Skip to content

Instantly share code, notes, and snippets.

@Madalosso
Created November 2, 2024 18:16
Show Gist options
  • Save Madalosso/eae673e51e5160c30835ce1f74926138 to your computer and use it in GitHub Desktop.
Save Madalosso/eae673e51e5160c30835ce1f74926138 to your computer and use it in GitHub Desktop.
Rust exercicio trait+generics ex 8
trait DescriptionStd {
fn description(&self) -> String {
String::from("No Description")
}
}
struct Elemento<T> {
item: T,
}
// Comentando funciona ok.
impl<T> DescriptionStd for Elemento<T> {
fn description(&self) -> String {
String::from("Tipo generico")
}
}
impl DescriptionStd for Elemento<i32> {
fn description(&self) -> String {
String::from("Elemento i32: ") + &self.item.to_string()
}
}
impl DescriptionStd for Elemento<String> {
fn description(&self) -> String {
String::from(self.item.as_str())
}
}
fn main() {
let elemento = Elemento { item: 10 };
println!("{}", elemento.description());
let elemento = Elemento {
item: String::from("tipo String"),
};
println!("{}", elemento.description());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment