Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
cff79ab5
R
Rust
项目概览
int
/
Rust
11 个月 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
cff79ab5
编写于
7月 02, 2014
作者:
P
Paolo Falabella
提交者:
Alex Crichton
7月 03, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rationals that have a 1 denom should print like ints
上级
faf5d926
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
28 addition
and
17 deletion
+28
-17
src/libnum/rational.rs
src/libnum/rational.rs
+28
-17
未找到文件。
src/libnum/rational.rs
浏览文件 @
cff79ab5
...
...
@@ -274,12 +274,17 @@ impl<T: Clone + Integer + PartialOrd>
Num
for
Ratio
<
T
>
{}
/* String conversions */
impl
<
T
:
fmt
::
Show
>
fmt
::
Show
for
Ratio
<
T
>
{
/// Renders as `numer/denom`.
impl
<
T
:
fmt
::
Show
+
Eq
+
One
>
fmt
::
Show
for
Ratio
<
T
>
{
/// Renders as `numer/denom`.
If denom=1, renders as numer.
fn
fmt
(
&
self
,
f
:
&
mut
fmt
::
Formatter
)
->
fmt
::
Result
{
write!
(
f
,
"{}/{}"
,
self
.numer
,
self
.denom
)
if
self
.denom
==
One
::
one
()
{
write!
(
f
,
"{}"
,
self
.numer
)
}
else
{
write!
(
f
,
"{}/{}"
,
self
.numer
,
self
.denom
)
}
}
}
impl
<
T
:
ToStrRadix
>
ToStrRadix
for
Ratio
<
T
>
{
/// Renders as `numer/denom` where the numbers are in base `radix`.
fn
to_str_radix
(
&
self
,
radix
:
uint
)
->
String
{
...
...
@@ -291,21 +296,20 @@ fn to_str_radix(&self, radix: uint) -> String {
impl
<
T
:
FromStr
+
Clone
+
Integer
+
PartialOrd
>
FromStr
for
Ratio
<
T
>
{
/// Parses `numer/denom`
.
/// Parses `numer/denom`
or just `numer`
fn
from_str
(
s
:
&
str
)
->
Option
<
Ratio
<
T
>>
{
let
split
:
Vec
<&
str
>
=
s
.splitn
(
'/'
,
1
)
.collect
();
if
split
.len
()
<
2
{
return
None
let
mut
split
=
s
.splitn
(
'/'
,
1
);
let
num
=
split
.next
()
.and_then
(|
n
|
FromStr
::
from_str
(
n
));
let
den
=
split
.next
()
.or
(
Some
(
"1"
))
.and_then
(|
d
|
FromStr
::
from_str
(
d
));
match
(
num
,
den
)
{
(
Some
(
n
),
Some
(
d
))
=>
Some
(
Ratio
::
new
(
n
,
d
)),
_
=>
None
}
let
a_option
:
Option
<
T
>
=
FromStr
::
from_str
(
*
split
.get
(
0
));
a_option
.and_then
(|
a
|
{
let
b_option
:
Option
<
T
>
=
FromStr
::
from_str
(
*
split
.get
(
1
));
b_option
.and_then
(|
b
|
{
Some
(
Ratio
::
new
(
a
.clone
(),
b
.clone
()))
})
})
}
}
impl
<
T
:
FromStrRadix
+
Clone
+
Integer
+
PartialOrd
>
FromStrRadix
for
Ratio
<
T
>
{
/// Parses `numer/denom` where the numbers are in base `radix`.
...
...
@@ -429,6 +433,13 @@ fn test_is_integer() {
assert
!
(
!
_
neg1_2
.is_integer
());
}
#[test]
fn
test_show
()
{
assert_eq!
(
format!
(
"{}"
,
_2
),
"2"
.to_string
());
assert_eq!
(
format!
(
"{}"
,
_1_2
),
"1/2"
.to_string
());
assert_eq!
(
format!
(
"{}"
,
_0
),
"0"
.to_string
());
assert_eq!
(
format!
(
"{}"
,
Ratio
::
from_integer
(
-
2
i
)),
"-2"
.to_string
());
}
mod
arith
{
use
super
::{
_0
,
_1
,
_2
,
_1_2
,
_3_2
,
_
neg1_2
,
to_big
};
...
...
@@ -562,11 +573,11 @@ fn test(r: Rational, s: String) {
assert_eq!
(
FromStr
::
from_str
(
s
.as_slice
()),
Some
(
r
));
assert_eq!
(
r
.to_str
(),
s
);
}
test
(
_1
,
"1
/1
"
.to_string
());
test
(
_0
,
"0
/1
"
.to_string
());
test
(
_1
,
"1"
.to_string
());
test
(
_0
,
"0"
.to_string
());
test
(
_1_2
,
"1/2"
.to_string
());
test
(
_3_2
,
"3/2"
.to_string
());
test
(
_2
,
"2
/1
"
.to_string
());
test
(
_2
,
"2"
.to_string
());
test
(
_
neg1_2
,
"-1/2"
.to_string
());
}
#[test]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录