🐮 Rust Traits Introduction Part I
In the Rust programming language, traits are very important. In this article I show you why and how you can use traits on a simple, yet clear example.
The Limitation Of The Basic Strict-Typing
Let’s start with basic strict-typing. Take a look at this example:
fn display(message: String) {
println!("You wrote: {}", message);
}
fn main() {
let name: String = String::from("Tom Smykowski");
display(name);
}
There the entry function called main(). In that function I am crating a String from the “Tom Smykowski” literal. Next, I am calling a function called display to display the text. The display function takes an argument message of String type and uses the built-in println! macro to print the text in the console. The first argument is a literal with a placeholder marked with {}. The placeholder is replaced by the value of the message argument that comes as the second parameter of the println! macro.
You can run the code, and it will display this output:
You wrote: Tom Smykowski
What is important in this example is that display function accepts an argument of type String. The same behavior you may…