Skip to content

Instantly share code, notes, and snippets.

@Kroisse
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Kroisse/29b5ef06dba7b40ca12c to your computer and use it in GitHub Desktop.
Save Kroisse/29b5ef06dba7b40ca12c to your computer and use it in GitHub Desktop.
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