Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
fn largest<T>(a: T, b: T) -> T {
if a > b { a } else { b }
}
fn main() {
println!("{}", largest(7, 3));
}It does not compile
- Expecting
7becausei32supports>: Rust checks a generic body once against its bounds, not per call as C++ templates do, soa > bon an unboundedTis error E0369. - Expecting a panic at run time: whether
Tsupports>is decided while building, so nothing runs untilT: PartialOrdis added.
- What is the smallest change that makes this program print
7? - Why does the error appear even when
largestis never called?