/gist:9c67e07666202d0358f8 Secret
Created
January 11, 2015 11:57
-
-
Save Regentag/9c67e07666202d0358f8 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
use std::cmp::Ordering::Less; | |
fn sort3<T: Ord>( a: T, b: T, c: T ) -> (T,T,T) | |
{ | |
// Ord trait는 cmp() 메서드를 선언한다. | |
match( a.cmp(&b), b.cmp(&c), c.cmp(&a) ) | |
{ | |
( Less, Less, _ ) => (a,b,c), | |
( _ , Less, Less ) => (b,c,a), | |
( Less, _ , Less ) => (c,a,b), | |
( Less, _ , _ ) => (a,c,b), | |
( _ , Less, _ ) => (b,a,c), | |
( _ , _ , Less ) => (c,b,a), | |
( _ , _ , _ ) => (a,b,c), // (Equal,Equal,Equal) | |
} | |
} | |
fn main() { | |
// 이하는 모두 Ord를 구현하는 타입의 값에 대한 테스트 | |
println!("{:?}", sort3("a", "b", "c")); | |
println!("{:?}", sort3(200, 101, 342)); | |
println!("{:?}", sort3("aa", "aaa", "a")); | |
println!("{:?}", sort3(-42, 3, 0)); | |
println!("{:?}", sort3("c", "b", "a")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment