@@ -274,12 +274,17 @@ impl<T: Clone + Integer + PartialOrd>
274
274
Num for Ratio < T > { }
275
275
276
276
/* 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.
279
279
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
+ }
281
285
}
282
286
}
287
+
283
288
impl < T : ToStrRadix > ToStrRadix for Ratio < T > {
284
289
/// Renders as `numer/denom` where the numbers are in base `radix`.
285
290
fn to_str_radix ( & self , radix : uint ) -> String {
@@ -291,21 +296,20 @@ impl<T: ToStrRadix> ToStrRadix for Ratio<T> {
291
296
292
297
impl < T : FromStr + Clone + Integer + PartialOrd >
293
298
FromStr for Ratio < T > {
294
- /// Parses `numer/denom`.
299
+ /// Parses `numer/denom` or just `numer`
295
300
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
299
309
}
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
- } )
307
310
}
308
311
}
312
+
309
313
impl < T : FromStrRadix + Clone + Integer + PartialOrd >
310
314
FromStrRadix for Ratio < T > {
311
315
/// Parses `numer/denom` where the numbers are in base `radix`.
@@ -429,6 +433,13 @@ mod test {
429
433
assert ! ( !_neg1_2. is_integer( ) ) ;
430
434
}
431
435
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
+ }
432
443
433
444
mod arith {
434
445
use super :: { _0, _1, _2, _1_2, _3_2, _neg1_2, to_big} ;
@@ -562,11 +573,11 @@ mod test {
562
573
assert_eq ! ( FromStr :: from_str( s. as_slice( ) ) , Some ( r) ) ;
563
574
assert_eq ! ( r. to_str( ) , s) ;
564
575
}
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 ( ) ) ;
567
578
test ( _1_2, "1/2" . to_string ( ) ) ;
568
579
test ( _3_2, "3/2" . to_string ( ) ) ;
569
- test ( _2, "2/1 " . to_string ( ) ) ;
580
+ test ( _2, "2" . to_string ( ) ) ;
570
581
test ( _neg1_2, "-1/2" . to_string ( ) ) ;
571
582
}
572
583
#[ test]
0 commit comments