<buttonid="sidebar-toggle"class="icon-button"type="button"title="Toggle Table of Contents"aria-label="Toggle Table of Contents"aria-controls="sidebar">
<inputtype="search"id="searchbar"name="searchbar"placeholder="Search this book ..."aria-controls="searchresults-outer"aria-describedby="searchresults-header">
<p>But what if you want to take more than just <code>i32</code>? You can use generics for this. Generics means "maybe one type, maybe another type".</p>
<p>For generics, you use angle brackets with the type inside, like this: <code><T></code> This means "any type you put into the function". Usually, generics uses types with one capital letter (T, U, V, etc.), though you don't have to just use one letter.</p>
<p>This is how you change the function to make it generic:</p>
<pre><preclass="playground"><codeclass="language-rust">fn return_number<T>(number: T) -> T {
println!("Here is your number.");
number
}
fn main() {
let number = return_number(5);
}
</code></pre></pre>
<p>The important part is the <code><T></code> after the function name. Without this, Rust will think that T is a concrete (concrete = not generic) type, like <code>String</code> or <code>i8</code>.</p>
<p>This is easier to understand if we write out a type name. See what happens when we change <code>T</code> to <code>MyType</code>:</p>
<p>So the single letter <code>T</code> is for human eyes, but the part after the function name is for the compiler's "eyes". Without it, it's not generic.</p>
<p>Now we will go back to type <code>T</code>, because Rust code usually uses <code>T</code>.</p>
<p>You will remember that some types in Rust are <strong>Copy</strong>, some are <strong>Clone</strong>, some are <strong>Display</strong>, some are <strong>Debug</strong>, and so on. With <strong>Debug</strong>, we can print with <code>{:?}</code>. So now you can see that we have a problem if we want to print <code>T</code>:</p>
println!("Here is your number: {:?}", number); // ⚠️
}
fn main() {
print_number(5);
}
</code></pre></pre>
<p><code>print_number</code> needs <strong>Debug</strong> to print <code>number</code>, but is <code>T</code> a type with <code>Debug</code>? Maybe not. Maybe it doesn't have <code>#[derive(Debug)]</code>, who knows. The compiler doesn't know either, so it gives an error:</p>
29 | println!("Here is your number: {:?}", number);
| ^^^^^^ `T` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
</code></pre>
<p>T doesn't implement <strong>Debug</strong>. So do we implement Debug for T? No, because we don't know what T is. But we can tell the function: "Don't worry, because any type T for this function will have Debug".</p>
<pre><preclass="playground"><codeclass="language-rust">use std::fmt::Debug; // Debug is located at std::fmt::Debug. So now we can just write 'Debug'.
fn print_number<T: Debug>(number: T) { // <T: Debug> is the important part
println!("Here is your number: {:?}", number);
}
fn main() {
print_number(5);
}
</code></pre></pre>
<p>So now the compiler knows: "Okay, this type T is going to have Debug". Now the code works, because <code>i32</code> has Debug. Now we can give it many types: <code>String</code>, <code>&str</code>, and so on, because they all have Debug.</p>
<p>Now we can create a struct and give it Debug with #[derive(Debug)], so now we can print it too. Our function can take <code>i32</code>, the struct Animal, and more:</p>
println!("Here is your item: {:?}", item);
}
fn main() {
let charlie = Animal {
name: "Charlie".to_string(),
age: 1,
};
let number = 55;
print_item(charlie);
print_item(number);
}
</code></pre></pre>
<p>This prints:</p>
<pre><codeclass="language-text">Here is your item: Animal { name: "Charlie", age: 1 }
Here is your item: 55
</code></pre>
<p>Sometimes we need more than one type in a generic function. We have to write out each type name, and think about how we want to use it. In this example, we want two types. First we want to print a statement for type T. Printing with <code>{}</code> is nicer, so we will require <code>Display</code> for <code>T</code>.</p>
<p>Next is type U, and the two variables <code>num_1</code> and <code>num_2</code> have type U (U is some sort of number). We want to compare them, so we need <code>PartialOrd</code>. That trait lets us use things like <code><</code>, <code>></code>, <code>==</code>, and so on. We want to print them too, so we require <code>Display</code> for <code>U</code> as well.</p>
<li>The function name is <code>compare_and_display</code>,</li>
<li>The first type is T, and it is generic. It must be a type that can print with {}.</li>
<li>The next type is U, and it is generic. It must be a type that can print with {}. Also, it must be a type that can compare (use <code>></code>, <code><</code>, and <code>==</code>).</li>
</ul>
<p>Now we can give <code>compare_and_display</code> different types. <code>statement</code> can be a <code>String</code>, a <code>&str</code>, anything with Display.</p>
<p>To make generic functions easier to read, we can also write it like this with <code>where</code> right before the code block:</p>