-
-
Save Kroisse/29b5ef06dba7b40ca12c to your computer and use it in GitHub Desktop.
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
fn sort3<T: TotalOrd>(a: T, b: T, c: T) -> (T, T, T) { | |
// TotalOrd trait는 cmp() 메서드를 선언한다 | |
match (a.cmp(&b), b.cmp(&c), a.cmp(&c)) { | |
(Less, Less, _ ) => (a, b, c), | |
(Less, _ , Less) => (a, c, b), | |
(Less, _ , _ ) => (c, a, b), | |
(_ , Less, _ ) => (b, a, c), | |
(_ , _ , Less) => (b, c, a), | |
(_ , _ , _ ) => (c, b, a), | |
} | |
} | |
fn main() { | |
// 이하는 모두 TotalOrd를 구현하는 타입의 값에 대한 테스트 | |
println!("{:?}", sort3("a", "b", "c")); | |
println!("{:?}", sort3(200, 101, 342)); | |
println!("{:?}", sort3("aa", "aaa", "a")); | |
println!("{:?}", sort3(-42, 3, 0)); | |
println!("{:?}", sort3(~[3], ~[], ~[1, 2])); | |
println!("{:?}", sort3("c", "b", "a")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment