@@ -274,12 +274,17 @@ impl<T: Clone + Integer + PartialOrd>
274274 Num for Ratio < T > { }
275275
276276/* String conversions */
277- impl < T : fmt:: Show > fmt:: Show for Ratio < T > {
278- /// Renders as `numer/denom`.
277+ impl < T : fmt:: Show + Eq + One > fmt:: Show for Ratio < T > {
278+ /// Renders as `numer/denom`. If denom=1, renders as numer.
279279 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
280- write ! ( f, "{}/{}" , self . numer, self . denom)
280+ if self . denom == One :: one ( ) {
281+ write ! ( f, "{}" , self . numer)
282+ } else {
283+ write ! ( f, "{}/{}" , self . numer, self . denom)
284+ }
281285 }
282286}
287+
283288impl < T : ToStrRadix > ToStrRadix for Ratio < T > {
284289 /// Renders as `numer/denom` where the numbers are in base `radix`.
285290 fn to_str_radix ( & self , radix : uint ) -> String {
@@ -291,21 +296,20 @@ impl<T: ToStrRadix> ToStrRadix for Ratio<T> {
291296
292297impl < T : FromStr + Clone + Integer + PartialOrd >
293298 FromStr for Ratio < T > {
294- /// Parses `numer/denom`.
299+ /// Parses `numer/denom` or just `numer`
295300 fn from_str ( s : & str ) -> Option < Ratio < T > > {
296- let split: Vec < & str > = s. splitn ( '/' , 1 ) . collect ( ) ;
297- if split. len ( ) < 2 {
298- return None
301+ let mut split = s. splitn ( '/' , 1 ) ;
302+
303+ let num = split. next ( ) . and_then ( |n| FromStr :: from_str ( n) ) ;
304+ let den = split. next ( ) . or ( Some ( "1" ) ) . and_then ( |d| FromStr :: from_str ( d) ) ;
305+
306+ match ( num, den) {
307+ ( Some ( n) , Some ( d) ) => Some ( Ratio :: new ( n, d) ) ,
308+ _ => None
299309 }
300- let a_option: Option < T > = FromStr :: from_str ( * split. get ( 0 ) ) ;
301- a_option. and_then ( |a| {
302- let b_option: Option < T > = FromStr :: from_str ( * split. get ( 1 ) ) ;
303- b_option. and_then ( |b| {
304- Some ( Ratio :: new ( a. clone ( ) , b. clone ( ) ) )
305- } )
306- } )
307310 }
308311}
312+
309313impl < T : FromStrRadix + Clone + Integer + PartialOrd >
310314 FromStrRadix for Ratio < T > {
311315 /// Parses `numer/denom` where the numbers are in base `radix`.
@@ -429,6 +433,13 @@ mod test {
429433 assert ! ( !_neg1_2. is_integer( ) ) ;
430434 }
431435
436+ #[ test]
437+ fn test_show ( ) {
438+ assert_eq ! ( format!( "{}" , _2) , "2" . to_string( ) ) ;
439+ assert_eq ! ( format!( "{}" , _1_2) , "1/2" . to_string( ) ) ;
440+ assert_eq ! ( format!( "{}" , _0) , "0" . to_string( ) ) ;
441+ assert_eq ! ( format!( "{}" , Ratio :: from_integer( -2 i) ) , "-2" . to_string( ) ) ;
442+ }
432443
433444 mod arith {
434445 use super :: { _0, _1, _2, _1_2, _3_2, _neg1_2, to_big} ;
@@ -562,11 +573,11 @@ mod test {
562573 assert_eq ! ( FromStr :: from_str( s. as_slice( ) ) , Some ( r) ) ;
563574 assert_eq ! ( r. to_str( ) , s) ;
564575 }
565- test ( _1, "1/1 " . to_string ( ) ) ;
566- test ( _0, "0/1 " . to_string ( ) ) ;
576+ test ( _1, "1" . to_string ( ) ) ;
577+ test ( _0, "0" . to_string ( ) ) ;
567578 test ( _1_2, "1/2" . to_string ( ) ) ;
568579 test ( _3_2, "3/2" . to_string ( ) ) ;
569- test ( _2, "2/1 " . to_string ( ) ) ;
580+ test ( _2, "2" . to_string ( ) ) ;
570581 test ( _neg1_2, "-1/2" . to_string ( ) ) ;
571582 }
572583 #[ test]
0 commit comments