2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-06-23 23:05:52 -05:00
|
|
|
// This file is intended to test only that methods are automatically
|
|
|
|
// reachable for each numeric type, for each exported impl, with no imports
|
|
|
|
// necessary. Testing the methods of the impls is done within the source
|
|
|
|
// file for each numeric type.
|
|
|
|
fn main() {
|
A new `times` method on numeric types
This method is intended to elegantly subsume two common iteration functions.
The first is `iter::range`, which is used identically to the method introduced
in this commit, but currently works only on uints. The second is a common case
of `{int, i8, uint, etc.}::range`, in the case where the inductive variable is
ignored. Compare the usage of the three:
```
for iter::range(100u) {
// do whatever
}
for int::range(0, 100) |_i| {
// do whatever
}
for 100.times {
// do whatever
}
```
I feel that the latter reads much more nicely than the first two approaches,
and unlike the first two the new method allows the user to ignore the specific
type of the number (ineed, if we're throwing away the inductive variable, who
cares what type it is?). A minor benefit is that this new method will be
somewhat familiar to users of Ruby, from which we borrow the name "times".
2012-07-05 21:12:26 -05:00
|
|
|
// ints
|
2012-06-25 15:41:13 -05:00
|
|
|
// num
|
2013-01-30 18:21:10 -06:00
|
|
|
assert 15i.add(&6) == 21;
|
2012-09-25 17:15:49 -05:00
|
|
|
assert 15i8.add(&6i8) == 21i8;
|
|
|
|
assert 15i16.add(&6i16) == 21i16;
|
|
|
|
assert 15i32.add(&6i32) == 21i32;
|
|
|
|
assert 15i64.add(&6i64) == 21i64;
|
2012-06-23 23:05:52 -05:00
|
|
|
|
A new `times` method on numeric types
This method is intended to elegantly subsume two common iteration functions.
The first is `iter::range`, which is used identically to the method introduced
in this commit, but currently works only on uints. The second is a common case
of `{int, i8, uint, etc.}::range`, in the case where the inductive variable is
ignored. Compare the usage of the three:
```
for iter::range(100u) {
// do whatever
}
for int::range(0, 100) |_i| {
// do whatever
}
for 100.times {
// do whatever
}
```
I feel that the latter reads much more nicely than the first two approaches,
and unlike the first two the new method allows the user to ignore the specific
type of the number (ineed, if we're throwing away the inductive variable, who
cares what type it is?). A minor benefit is that this new method will be
somewhat familiar to users of Ruby, from which we borrow the name "times".
2012-07-05 21:12:26 -05:00
|
|
|
// uints
|
2012-06-25 15:41:13 -05:00
|
|
|
// num
|
2012-09-25 17:15:49 -05:00
|
|
|
assert 15u.add(&6u) == 21u;
|
|
|
|
assert 15u8.add(&6u8) == 21u8;
|
|
|
|
assert 15u16.add(&6u16) == 21u16;
|
|
|
|
assert 15u32.add(&6u32) == 21u32;
|
|
|
|
assert 15u64.add(&6u64) == 21u64;
|
2013-01-30 18:21:10 -06:00
|
|
|
|
A new `times` method on numeric types
This method is intended to elegantly subsume two common iteration functions.
The first is `iter::range`, which is used identically to the method introduced
in this commit, but currently works only on uints. The second is a common case
of `{int, i8, uint, etc.}::range`, in the case where the inductive variable is
ignored. Compare the usage of the three:
```
for iter::range(100u) {
// do whatever
}
for int::range(0, 100) |_i| {
// do whatever
}
for 100.times {
// do whatever
}
```
I feel that the latter reads much more nicely than the first two approaches,
and unlike the first two the new method allows the user to ignore the specific
type of the number (ineed, if we're throwing away the inductive variable, who
cares what type it is?). A minor benefit is that this new method will be
somewhat familiar to users of Ruby, from which we borrow the name "times".
2012-07-05 21:12:26 -05:00
|
|
|
// times
|
2012-07-09 21:12:50 -05:00
|
|
|
15u.times(|| false);
|
2012-06-23 23:05:52 -05:00
|
|
|
|
A new `times` method on numeric types
This method is intended to elegantly subsume two common iteration functions.
The first is `iter::range`, which is used identically to the method introduced
in this commit, but currently works only on uints. The second is a common case
of `{int, i8, uint, etc.}::range`, in the case where the inductive variable is
ignored. Compare the usage of the three:
```
for iter::range(100u) {
// do whatever
}
for int::range(0, 100) |_i| {
// do whatever
}
for 100.times {
// do whatever
}
```
I feel that the latter reads much more nicely than the first two approaches,
and unlike the first two the new method allows the user to ignore the specific
type of the number (ineed, if we're throwing away the inductive variable, who
cares what type it is?). A minor benefit is that this new method will be
somewhat familiar to users of Ruby, from which we borrow the name "times".
2012-07-05 21:12:26 -05:00
|
|
|
// floats
|
2012-06-25 15:41:13 -05:00
|
|
|
// num
|
2012-06-23 23:05:52 -05:00
|
|
|
assert 10f.to_int() == 10;
|
|
|
|
assert 10f32.to_int() == 10;
|
|
|
|
assert 10f64.to_int() == 10;
|
|
|
|
}
|