Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUST

armanriazi•rust•trait•bound

In the implementation of outline_print, we want to use the Display trait’s functionality. Therefore, we need to specify that the OutlinePrint trait will work only for types that also implement Display and provide the functionality that OutlinePrint needs. We can do that in the trait definition by specifying OutlinePrint: Display. 
This technique is similar to adding a trait bound to the trait.
we can use the to_string function that is automatically implemented for any type that implements Display.
use std::fmt;
trait OutlinePrint: fmt::Display {
    fn outline_print(&self) {  let output = self.to_string();}
}
 
PREVIOUS NEXT
Tagged:
ADD COMMENT
Topic
Name
2+7 =