Created
November 2, 2024 18:16
-
-
Save Madalosso/eae673e51e5160c30835ce1f74926138 to your computer and use it in GitHub Desktop.
Rust exercicio trait+generics ex 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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