diff --git a/configure b/configure
index a3291725703..f070ae37dda 100755
--- a/configure
+++ b/configure
@@ -739,6 +739,7 @@ do
make_dir $h/test/doc-tutorial-ffi
make_dir $h/test/doc-tutorial-macros
make_dir $h/test/doc-tutorial-borrowed-ptr
+ make_dir $h/test/doc-tutorial-container
make_dir $h/test/doc-tutorial-tasks
make_dir $h/test/doc-tutorial-conditions
make_dir $h/test/doc-rust
diff --git a/doc/rust.md b/doc/rust.md
index 8b06c170f03..4cd9dc3a116 100644
--- a/doc/rust.md
+++ b/doc/rust.md
@@ -2864,17 +2864,16 @@ the vtable pointer for the `T` implementation of `R`, and the pointer value of `
An example of an object type:
~~~~~~~~
-# use std::int;
trait Printable {
- fn to_str(&self) -> ~str;
+ fn to_string(&self) -> ~str;
}
impl Printable for int {
- fn to_str(&self) -> ~str { int::to_str(*self) }
+ fn to_string(&self) -> ~str { self.to_str() }
}
fn print(a: @Printable) {
- println(a.to_str());
+ println(a.to_string());
}
fn main() {
diff --git a/doc/tutorial-container.md b/doc/tutorial-container.md
index 1f47c3df14f..37ca561f74a 100644
--- a/doc/tutorial-container.md
+++ b/doc/tutorial-container.md
@@ -163,7 +163,7 @@ assert_eq!(sum, 57);
The `for` keyword can be used as sugar for iterating through any iterator:
~~~
-let xs = [2, 3, 5, 7, 11, 13, 17];
+let xs = [2u, 3, 5, 7, 11, 13, 17];
// print out all the elements in the vector
for x in xs.iter() {
@@ -219,7 +219,7 @@ Containers can provide conversion from iterators through `collect` by
implementing the `FromIterator` trait. For example, the implementation for
vectors is as follows:
-~~~
+~~~ {.xfail-test}
impl FromIterator for ~[A] {
pub fn from_iterator>(iterator: &mut T) -> ~[A] {
let (lower, _) = iterator.size_hint();
@@ -237,7 +237,7 @@ impl FromIterator for ~[A] {
The `Iterator` trait provides a `size_hint` default method, returning a lower
bound and optionally on upper bound on the length of the iterator:
-~~~
+~~~ {.xfail-test}
fn size_hint(&self) -> (uint, Option) { (0, None) }
~~~
@@ -319,6 +319,16 @@ for x in it.invert() {
}
~~~
+The `reverse_` method is also available for any double-ended iterator yielding
+mutable references. It can be used to reverse a container in-place. Note that
+the trailing underscore is a workaround for issue #5898 and will be removed.
+
+~~~
+let mut ys = [1, 2, 3, 4, 5];
+ys.mut_iter().reverse_();
+assert_eq!(ys, [5, 4, 3, 2, 1]);
+~~~
+
## Random-access iterators
The `RandomAccessIterator` trait represents an iterator offering random access
diff --git a/doc/tutorial.md b/doc/tutorial.md
index 1813356a1f3..958c1573761 100644
--- a/doc/tutorial.md
+++ b/doc/tutorial.md
@@ -555,12 +555,11 @@ while cake_amount > 0 {
`loop` denotes an infinite loop, and is the preferred way of writing `while true`:
~~~~
-use std::int;
-let mut x = 5;
+let mut x = 5u;
loop {
x += x - 3;
if x % 5 == 0 { break; }
- println(int::to_str(x));
+ println(x.to_str());
}
~~~~
diff --git a/mk/tests.mk b/mk/tests.mk
index 4732f808457..02fcf61412f 100644
--- a/mk/tests.mk
+++ b/mk/tests.mk
@@ -20,7 +20,7 @@ TEST_CRATES = $(TEST_TARGET_CRATES) $(TEST_HOST_CRATES)
# Markdown files under doc/ that should have their code extracted and run
DOC_TEST_NAMES = tutorial tutorial-ffi tutorial-macros tutorial-borrowed-ptr \
- tutorial-tasks tutorial-conditions rust
+ tutorial-tasks tutorial-conditions tutorial-container rust
######################################################################
# Environment configuration
diff --git a/src/libextra/crypto/digest.rs b/src/libextra/crypto/digest.rs
index c7f228af332..d2d6b540cff 100644
--- a/src/libextra/crypto/digest.rs
+++ b/src/libextra/crypto/digest.rs
@@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-use std::uint;
use std::vec;
@@ -71,7 +70,7 @@ pub trait Digest {
fn to_hex(rr: &[u8]) -> ~str {
let mut s = ~"";
for b in rr.iter() {
- let hex = uint::to_str_radix(*b as uint, 16u);
+ let hex = (*b as uint).to_str_radix(16u);
if hex.len() == 1 {
s.push_char('0');
}
diff --git a/src/libextra/extra.rs b/src/libextra/extra.rs
index da6525f7815..caf2c41d31d 100644
--- a/src/libextra/extra.rs
+++ b/src/libextra/extra.rs
@@ -55,7 +55,6 @@ pub mod flatpipes;
pub mod container;
pub mod bitv;
-pub mod fun_treemap;
pub mod list;
pub mod ringbuf;
pub mod priority_queue;
diff --git a/src/libextra/fun_treemap.rs b/src/libextra/fun_treemap.rs
deleted file mode 100644
index edbe323ec2d..00000000000
--- a/src/libextra/fun_treemap.rs
+++ /dev/null
@@ -1,84 +0,0 @@
-// 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 or the MIT license
-// , at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-/*!
- * A functional key,value store that works on anything.
- *
- * This works using a binary search tree. In the first version, it's a
- * very naive algorithm, but it will probably be updated to be a
- * red-black tree or something else.
- *
- * This is copied and modified from treemap right now. It's missing a lot
- * of features.
- */
-
-
-use std::cmp::{Eq, Ord};
-use std::option::{Some, None};
-
-pub type Treemap = @TreeNode;
-
-enum TreeNode {
- Empty,
- Node(@K, @V, @TreeNode, @TreeNode)
-}
-
-/// Create a treemap
-pub fn init() -> Treemap {
- @Empty
-}
-
-/// Insert a value into the map
-pub fn insert(
- m: Treemap,
- k: K,
- v: V)
- -> Treemap {
- @match m {
- @Empty => Node(@k, @v, @Empty, @Empty),
- @Node(kk, vv, left, right) => cond!(
- (k < *kk) { Node(kk, vv, insert(left, k, v), right) }
- (k == *kk) { Node(kk, @v, left, right) }
- _ { Node(kk, vv, left, insert(right, k, v)) }
- )
- }
-}
-
-/// Find a value based on the key
-pub fn find(
- m: Treemap,
- k: K)
- -> Option {
- match *m {
- Empty => None,
- Node(kk, v, left, right) => cond!(
- (k == *kk) { Some((*v).clone()) }
- (k < *kk) { find(left, k) }
- _ { find(right, k) }
- )
- }
-}
-
-/// Visit all pairs in the map in order.
-pub fn traverse(m: Treemap, f: &fn(&K, &V)) {
- match *m {
- Empty => (),
- // Previously, this had what looked like redundant
- // matches to me, so I changed it. but that may be a
- // de-optimization -- tjc
- Node(@ref k, @ref v, left, right) => {
- traverse(left, |k,v| f(k,v));
- f(k, v);
- traverse(right, |k,v| f(k,v));
- }
- }
-}
diff --git a/src/libextra/md4.rs b/src/libextra/md4.rs
index 2c60be44519..abce22f98c6 100644
--- a/src/libextra/md4.rs
+++ b/src/libextra/md4.rs
@@ -9,7 +9,6 @@
// except according to those terms.
-use std::uint;
use std::vec;
struct Quad {
@@ -121,7 +120,7 @@ pub fn md4_str(msg: &[u8]) -> ~str {
if byte <= 16u8 {
result.push_char('0')
}
- result.push_str(uint::to_str_radix(byte as uint, 16u));
+ result.push_str((byte as uint).to_str_radix(16u));
i += 1u32;
}
}
diff --git a/src/libextra/num/bigint.rs b/src/libextra/num/bigint.rs
index 354696ef420..c86c4dd07ed 100644
--- a/src/libextra/num/bigint.rs
+++ b/src/libextra/num/bigint.rs
@@ -525,7 +525,7 @@ impl ToStrRadix for BigUint {
if v.is_empty() { return ~"0" }
let mut s = str::with_capacity(v.len() * l);
for n in v.rev_iter() {
- let ss = uint::to_str_radix(*n as uint, radix);
+ let ss = (*n as uint).to_str_radix(radix);
s.push_str("0".repeat(l - ss.len()));
s.push_str(ss);
}
diff --git a/src/libextra/time.rs b/src/libextra/time.rs
index ab35bf2386c..257d941e4af 100644
--- a/src/libextra/time.rs
+++ b/src/libextra/time.rs
@@ -10,8 +10,6 @@
#[allow(missing_doc)];
-
-use std::int;
use std::io;
use std::num;
use std::str;
@@ -824,7 +822,7 @@ fn do_strftime(format: &str, tm: &Tm) -> ~str {
//'U' {}
'u' => {
let i = tm.tm_wday as int;
- int::to_str(if i == 0 { 7 } else { i })
+ (if i == 0 { 7 } else { i }).to_str()
}
//'V' {}
'v' => {
@@ -834,10 +832,10 @@ fn do_strftime(format: &str, tm: &Tm) -> ~str {
parse_type('Y', tm))
}
//'W' {}
- 'w' => int::to_str(tm.tm_wday as int),
+ 'w' => (tm.tm_wday as int).to_str(),
//'X' {}
//'x' {}
- 'Y' => int::to_str(tm.tm_year as int + 1900),
+ 'Y' => (tm.tm_year as int + 1900).to_str(),
'y' => fmt!("%02d", (tm.tm_year as int + 1900) % 100),
'Z' => tm.tm_zone.clone(),
'z' => {
diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs
index cdafb7400e1..1ddebbb4280 100644
--- a/src/librustc/driver/driver.rs
+++ b/src/librustc/driver/driver.rs
@@ -26,7 +26,6 @@ use util::common::time;
use util::ppaux;
use std::hashmap::{HashMap,HashSet};
-use std::int;
use std::io;
use std::os;
use std::vec;
@@ -454,21 +453,21 @@ pub fn pretty_print_input(sess: Session, cfg: ast::CrateConfig, input: &input,
match node {
pprust::node_item(s, item) => {
pp::space(s.s);
- pprust::synth_comment(s, int::to_str(item.id));
+ pprust::synth_comment(s, item.id.to_str());
}
pprust::node_block(s, ref blk) => {
pp::space(s.s);
pprust::synth_comment(
- s, ~"block " + int::to_str(blk.id));
+ s, ~"block " + blk.id.to_str());
}
pprust::node_expr(s, expr) => {
pp::space(s.s);
- pprust::synth_comment(s, int::to_str(expr.id));
+ pprust::synth_comment(s, expr.id.to_str());
pprust::pclose(s);
}
pprust::node_pat(s, pat) => {
pp::space(s.s);
- pprust::synth_comment(s, ~"pat " + int::to_str(pat.id));
+ pprust::synth_comment(s, ~"pat " + pat.id.to_str());
}
}
}
diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs
index 4a3704dc3aa..eba01c8d399 100644
--- a/src/librustc/metadata/encoder.rs
+++ b/src/librustc/metadata/encoder.rs
@@ -25,7 +25,6 @@ use std::hash::HashUtil;
use std::hashmap::{HashMap, HashSet};
use std::io;
use std::str;
-use std::uint;
use std::vec;
use extra::flate;
use extra::serialize::Encodable;
@@ -303,7 +302,7 @@ fn encode_disr_val(_: &EncodeContext,
ebml_w: &mut writer::Encoder,
disr_val: uint) {
ebml_w.start_tag(tag_disr_val);
- let s = uint::to_str(disr_val);
+ let s = disr_val.to_str();
ebml_w.writer.write(s.as_bytes());
ebml_w.end_tag();
}
diff --git a/src/librustc/metadata/tyencode.rs b/src/librustc/metadata/tyencode.rs
index 915729d254f..5611808cc6d 100644
--- a/src/librustc/metadata/tyencode.rs
+++ b/src/librustc/metadata/tyencode.rs
@@ -17,7 +17,6 @@ use middle::ty;
use std::hashmap::HashMap;
use std::io::WriterUtil;
use std::io;
-use std::uint;
use syntax::abi::AbiSet;
use syntax::ast;
use syntax::ast::*;
@@ -324,7 +323,7 @@ fn enc_sty(w: @io::Writer, cx: @ctxt, st: &ty::sty) {
w.write_char('p');
w.write_str((cx.ds)(did));
w.write_char('|');
- w.write_str(uint::to_str(id));
+ w.write_str(id.to_str());
}
ty::ty_self(did) => {
w.write_char('s');
diff --git a/src/librustc/middle/trans/base.rs b/src/librustc/middle/trans/base.rs
index 95021d3b6e7..50b5140505e 100644
--- a/src/librustc/middle/trans/base.rs
+++ b/src/librustc/middle/trans/base.rs
@@ -71,7 +71,6 @@ use std::hash;
use std::hashmap::HashMap;
use std::io;
use std::libc::c_uint;
-use std::uint;
use std::vec;
use std::local_data;
use extra::time;
@@ -719,7 +718,7 @@ pub fn iter_structural_ty(cx: @mut Block, av: ValueRef, t: ty::t,
for variant in (*variants).iter() {
let variant_cx =
sub_block(cx, ~"enum-iter-variant-" +
- uint::to_str(variant.disr_val));
+ variant.disr_val.to_str());
let variant_cx =
iter_variant(variant_cx, repr, av, *variant,
substs.tps, |x,y,z| f(x,y,z));
diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs
index 67bd8782497..96d12dbc3ac 100644
--- a/src/librustc/middle/ty.rs
+++ b/src/librustc/middle/ty.rs
@@ -33,7 +33,6 @@ use std::ops;
use std::ptr::to_unsafe_ptr;
use std::to_bytes;
use std::to_str::ToStr;
-use std::u32;
use std::vec;
use syntax::ast::*;
use syntax::ast_util::is_local;
@@ -1944,7 +1943,7 @@ impl ops::Sub for TypeContents {
impl ToStr for TypeContents {
fn to_str(&self) -> ~str {
- fmt!("TypeContents(%s)", u32::to_str_radix(self.bits, 2))
+ fmt!("TypeContents(%s)", self.bits.to_str_radix(2))
}
}
diff --git a/src/librustc/middle/typeck/infer/to_str.rs b/src/librustc/middle/typeck/infer/to_str.rs
index 18594f35295..e2d6d588bae 100644
--- a/src/librustc/middle/typeck/infer/to_str.rs
+++ b/src/librustc/middle/typeck/infer/to_str.rs
@@ -17,7 +17,6 @@ use middle::typeck::infer::InferCtxt;
use middle::typeck::infer::unify::{Redirect, Root, VarValue};
use util::ppaux::{mt_to_str, ty_to_str, trait_ref_to_str};
-use std::uint;
use syntax::ast;
pub trait InferStr {
@@ -72,7 +71,7 @@ impl InferStr for VarValue {
match *self {
Redirect(ref vid) => fmt!("Redirect(%s)", vid.to_str()),
Root(ref pt, rk) => fmt!("Root(%s, %s)", pt.inf_str(cx),
- uint::to_str_radix(rk, 10u))
+ rk.to_str_radix(10u))
}
}
}
diff --git a/src/libstd/container.rs b/src/libstd/container.rs
index 22525d4cab4..c91a53f9663 100644
--- a/src/libstd/container.rs
+++ b/src/libstd/container.rs
@@ -38,6 +38,7 @@ pub trait Map: Container {
fn find<'a>(&'a self, key: &K) -> Option<&'a V>;
/// Return true if the map contains a value for the specified key
+ #[inline]
fn contains_key(&self, key: &K) -> bool {
self.find(key).is_some()
}
diff --git a/src/libstd/hash.rs b/src/libstd/hash.rs
index f3df42f7a43..21b7ee321e8 100644
--- a/src/libstd/hash.rs
+++ b/src/libstd/hash.rs
@@ -27,8 +27,8 @@ use option::{Some, None};
use rt::io::Writer;
use str::OwnedStr;
use to_bytes::IterBytes;
-use uint;
use vec::ImmutableVector;
+use num::ToStrRadix;
// Alias `SipState` to `State`.
pub use State = hash::SipState;
@@ -386,7 +386,7 @@ impl Streaming for SipState {
let r = self.result_bytes();
let mut s = ~"";
for b in r.iter() {
- s.push_str(uint::to_str_radix(*b as uint, 16u));
+ s.push_str((*b as uint).to_str_radix(16u));
}
s
}
@@ -407,8 +407,6 @@ mod tests {
use super::*;
use prelude::*;
- use uint;
-
// Hash just the bytes of the slice, without length prefix
struct Bytes<'self>(&'self [u8]);
impl<'self> IterBytes for Bytes<'self> {
@@ -496,7 +494,7 @@ mod tests {
fn to_hex_str(r: &[u8, ..8]) -> ~str {
let mut s = ~"";
for b in r.iter() {
- s.push_str(uint::to_str_radix(*b as uint, 16u));
+ s.push_str((*b as uint).to_str_radix(16u));
}
s
}
diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs
index 1d32c5df14e..36e62856464 100644
--- a/src/libstd/iterator.rs
+++ b/src/libstd/iterator.rs
@@ -24,6 +24,7 @@ use ops::{Add, Mul, Sub};
use cmp::Ord;
use clone::Clone;
use uint;
+use util;
/// Conversion from an `Iterator`
pub trait FromIterator {
@@ -583,6 +584,26 @@ pub trait DoubleEndedIterator: Iterator {
}
}
+/// A double-ended iterator yielding mutable references
+pub trait MutableDoubleEndedIterator {
+ // FIXME: #5898: should be called `reverse`
+ /// Use an iterator to reverse a container in-place
+ fn reverse_(&mut self);
+}
+
+impl<'self, A, T: DoubleEndedIterator<&'self mut A>> MutableDoubleEndedIterator for T {
+ // FIXME: #5898: should be called `reverse`
+ /// Use an iterator to reverse a container in-place
+ fn reverse_(&mut self) {
+ loop {
+ match (self.next(), self.next_back()) {
+ (Some(x), Some(y)) => util::swap(x, y),
+ _ => break
+ }
+ }
+ }
+}
+
/// An object implementing random access indexing by `uint`
///
/// A `RandomAccessIterator` should be either infinite or a `DoubleEndedIterator`.
@@ -1522,6 +1543,52 @@ impl + Integer + Ord + Clone> DoubleEndedIterator for Range {
}
}
+/// A range of numbers from [0, N]
+#[deriving(Clone, DeepClone)]
+pub struct RangeInclusive {
+ priv range: Range,
+ priv done: bool
+}
+
+/// Return an iterator over the range [start, stop]
+#[inline]
+pub fn range_inclusive + Ord + Clone + One>(start: A, stop: A) -> RangeInclusive {
+ RangeInclusive{range: range(start, stop), done: false}
+}
+
+impl + Ord + Clone> Iterator for RangeInclusive {
+ #[inline]
+ fn next(&mut self) -> Option {
+ match self.range.next() {
+ Some(x) => Some(x),
+ None => {
+ if self.done {
+ None
+ } else {
+ self.done = true;
+ Some(self.range.stop.clone())
+ }
+ }
+ }
+ }
+}
+
+impl + Integer + Ord + Clone> DoubleEndedIterator for RangeInclusive {
+ #[inline]
+ fn next_back(&mut self) -> Option {
+ if self.range.stop > self.range.state {
+ let result = self.range.stop.clone();
+ self.range.stop = self.range.stop - self.range.one;
+ Some(result)
+ } else if self.done {
+ None
+ } else {
+ self.done = true;
+ Some(self.range.stop.clone())
+ }
+ }
+}
+
impl + Clone> Iterator for Counter {
#[inline]
fn next(&mut self) -> Option {
@@ -2286,4 +2353,17 @@ mod tests {
fail!("unreachable");
}
}
+
+ #[test]
+ fn test_range_inclusive() {
+ assert_eq!(range_inclusive(0i, 5).collect::<~[int]>(), ~[0i, 1, 2, 3, 4, 5]);
+ assert_eq!(range_inclusive(0i, 5).invert().collect::<~[int]>(), ~[5i, 4, 3, 2, 1, 0]);
+ }
+
+ #[test]
+ fn test_reverse() {
+ let mut ys = [1, 2, 3, 4, 5];
+ ys.mut_iter().reverse_();
+ assert_eq!(ys, [5, 4, 3, 2, 1]);
+ }
}
diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs
index 17175de9b92..a493dba467e 100644
--- a/src/libstd/num/f32.rs
+++ b/src/libstd/num/f32.rs
@@ -740,29 +740,6 @@ pub fn to_str_hex(num: f32) -> ~str {
r
}
-///
-/// Converts a float to a string in a given radix
-///
-/// # Arguments
-///
-/// * num - The float value
-/// * radix - The base to use
-///
-/// # Failure
-///
-/// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
-/// possible misinterpretation of the result at higher bases. If those values
-/// are expected, use `to_str_radix_special()` instead.
-///
-#[inline]
-pub fn to_str_radix(num: f32, rdx: uint) -> ~str {
- let (r, special) = strconv::float_to_str_common(
- num, rdx, true, strconv::SignNeg, strconv::DigAll);
- if special { fail!("number has a special value, \
- try to_str_radix_special() if those are expected") }
- r
-}
-
///
/// Converts a float to a string in a given radix, and a flag indicating
/// whether it's a special value
@@ -816,9 +793,25 @@ impl to_str::ToStr for f32 {
}
impl num::ToStrRadix for f32 {
+ /// Converts a float to a string in a given radix
+ ///
+ /// # Arguments
+ ///
+ /// * num - The float value
+ /// * radix - The base to use
+ ///
+ /// # Failure
+ ///
+ /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
+ /// possible misinterpretation of the result at higher bases. If those values
+ /// are expected, use `to_str_radix_special()` instead.
#[inline]
fn to_str_radix(&self, rdx: uint) -> ~str {
- to_str_radix(*self, rdx)
+ let (r, special) = strconv::float_to_str_common(
+ *self, rdx, true, strconv::SignNeg, strconv::DigAll);
+ if special { fail!("number has a special value, \
+ try to_str_radix_special() if those are expected") }
+ r
}
}
diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs
index 91361a61c21..52e74d969eb 100644
--- a/src/libstd/num/f64.rs
+++ b/src/libstd/num/f64.rs
@@ -787,29 +787,6 @@ pub fn to_str_hex(num: f64) -> ~str {
r
}
-///
-/// Converts a float to a string in a given radix
-///
-/// # Arguments
-///
-/// * num - The float value
-/// * radix - The base to use
-///
-/// # Failure
-///
-/// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
-/// possible misinterpretation of the result at higher bases. If those values
-/// are expected, use `to_str_radix_special()` instead.
-///
-#[inline]
-pub fn to_str_radix(num: f64, rdx: uint) -> ~str {
- let (r, special) = strconv::float_to_str_common(
- num, rdx, true, strconv::SignNeg, strconv::DigAll);
- if special { fail!("number has a special value, \
- try to_str_radix_special() if those are expected") }
- r
-}
-
///
/// Converts a float to a string in a given radix, and a flag indicating
/// whether it's a special value
@@ -863,9 +840,25 @@ impl to_str::ToStr for f64 {
}
impl num::ToStrRadix for f64 {
+ /// Converts a float to a string in a given radix
+ ///
+ /// # Arguments
+ ///
+ /// * num - The float value
+ /// * radix - The base to use
+ ///
+ /// # Failure
+ ///
+ /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
+ /// possible misinterpretation of the result at higher bases. If those values
+ /// are expected, use `to_str_radix_special()` instead.
#[inline]
fn to_str_radix(&self, rdx: uint) -> ~str {
- to_str_radix(*self, rdx)
+ let (r, special) = strconv::float_to_str_common(
+ *self, rdx, true, strconv::SignNeg, strconv::DigAll);
+ if special { fail!("number has a special value, \
+ try to_str_radix_special() if those are expected") }
+ r
}
}
diff --git a/src/libstd/num/float.rs b/src/libstd/num/float.rs
index 486d3562089..20c7adbd62c 100644
--- a/src/libstd/num/float.rs
+++ b/src/libstd/num/float.rs
@@ -111,29 +111,6 @@ pub fn to_str_hex(num: float) -> ~str {
r
}
-///
-/// Converts a float to a string in a given radix
-///
-/// # Arguments
-///
-/// * num - The float value
-/// * radix - The base to use
-///
-/// # Failure
-///
-/// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
-/// possible misinterpretation of the result at higher bases. If those values
-/// are expected, use `to_str_radix_special()` instead.
-///
-#[inline]
-pub fn to_str_radix(num: float, radix: uint) -> ~str {
- let (r, special) = strconv::float_to_str_common(
- num, radix, true, strconv::SignNeg, strconv::DigAll);
- if special { fail!("number has a special value, \
- try to_str_radix_special() if those are expected") }
- r
-}
-
///
/// Converts a float to a string in a given radix, and a flag indicating
/// whether it's a special value
@@ -187,9 +164,25 @@ impl to_str::ToStr for float {
}
impl num::ToStrRadix for float {
+ /// Converts a float to a string in a given radix
+ ///
+ /// # Arguments
+ ///
+ /// * num - The float value
+ /// * radix - The base to use
+ ///
+ /// # Failure
+ ///
+ /// Fails if called on a special value like `inf`, `-inf` or `NaN` due to
+ /// possible misinterpretation of the result at higher bases. If those values
+ /// are expected, use `to_str_radix_special()` instead.
#[inline]
fn to_str_radix(&self, radix: uint) -> ~str {
- to_str_radix(*self, radix)
+ let (r, special) = strconv::float_to_str_common(
+ *self, radix, true, strconv::SignNeg, strconv::DigAll);
+ if special { fail!("number has a special value, \
+ try to_str_radix_special() if those are expected") }
+ r
}
}
@@ -1342,8 +1335,8 @@ mod tests {
#[test]
pub fn test_to_str_radix() {
- assert_eq!(to_str_radix(36., 36u), ~"10");
- assert_eq!(to_str_radix(8.125, 2u), ~"1000.001");
+ assert_eq!(36.0f.to_str_radix(36u), ~"10");
+ assert_eq!(8.125f.to_str_radix(2u), ~"1000.001");
}
#[test]
diff --git a/src/libstd/num/int_macros.rs b/src/libstd/num/int_macros.rs
index 0144f926534..4a7a5e32b32 100644
--- a/src/libstd/num/int_macros.rs
+++ b/src/libstd/num/int_macros.rs
@@ -525,35 +525,25 @@ pub fn to_str_bytes(n: $T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
f(buf.slice(0, cur))
}
-/// Convert to a string in base 10.
-#[inline]
-pub fn to_str(num: $T) -> ~str {
- to_str_radix(num, 10u)
-}
-
-/// Convert to a string in a given base.
-#[inline]
-pub fn to_str_radix(num: $T, radix: uint) -> ~str {
- let mut buf: ~[u8] = ~[];
- do strconv::int_to_str_bytes_common(num, radix, strconv::SignNeg) |i| {
- buf.push(i);
- }
- // We know we generated valid utf-8, so we don't need to go through that
- // check.
- unsafe { str::raw::from_bytes_owned(buf) }
-}
-
impl ToStr for $T {
+ /// Convert to a string in base 10.
#[inline]
fn to_str(&self) -> ~str {
- to_str(*self)
+ self.to_str_radix(10)
}
}
impl ToStrRadix for $T {
+ /// Convert to a string in a given base.
#[inline]
fn to_str_radix(&self, radix: uint) -> ~str {
- to_str_radix(*self, radix)
+ let mut buf: ~[u8] = ~[];
+ do strconv::int_to_str_bytes_common(*self, radix, strconv::SignNeg) |i| {
+ buf.push(i);
+ }
+ // We know we generated valid utf-8, so we don't need to go through that
+ // check.
+ unsafe { str::raw::from_bytes_owned(buf) }
}
}
@@ -813,39 +803,39 @@ mod tests {
#[test]
fn test_to_str() {
- assert_eq!(to_str_radix(0 as $T, 10u), ~"0");
- assert_eq!(to_str_radix(1 as $T, 10u), ~"1");
- assert_eq!(to_str_radix(-1 as $T, 10u), ~"-1");
- assert_eq!(to_str_radix(127 as $T, 16u), ~"7f");
- assert_eq!(to_str_radix(100 as $T, 10u), ~"100");
+ assert_eq!((0 as $T).to_str_radix(10u), ~"0");
+ assert_eq!((1 as $T).to_str_radix(10u), ~"1");
+ assert_eq!((-1 as $T).to_str_radix(10u), ~"-1");
+ assert_eq!((127 as $T).to_str_radix(16u), ~"7f");
+ assert_eq!((100 as $T).to_str_radix(10u), ~"100");
}
#[test]
fn test_int_to_str_overflow() {
let mut i8_val: i8 = 127_i8;
- assert_eq!(i8::to_str(i8_val), ~"127");
+ assert_eq!(i8_val.to_str(), ~"127");
i8_val += 1 as i8;
- assert_eq!(i8::to_str(i8_val), ~"-128");
+ assert_eq!(i8_val.to_str(), ~"-128");
let mut i16_val: i16 = 32_767_i16;
- assert_eq!(i16::to_str(i16_val), ~"32767");
+ assert_eq!(i16_val.to_str(), ~"32767");
i16_val += 1 as i16;
- assert_eq!(i16::to_str(i16_val), ~"-32768");
+ assert_eq!(i16_val.to_str(), ~"-32768");
let mut i32_val: i32 = 2_147_483_647_i32;
- assert_eq!(i32::to_str(i32_val), ~"2147483647");
+ assert_eq!(i32_val.to_str(), ~"2147483647");
i32_val += 1 as i32;
- assert_eq!(i32::to_str(i32_val), ~"-2147483648");
+ assert_eq!(i32_val.to_str(), ~"-2147483648");
let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
- assert_eq!(i64::to_str(i64_val), ~"9223372036854775807");
+ assert_eq!(i64_val.to_str(), ~"9223372036854775807");
i64_val += 1 as i64;
- assert_eq!(i64::to_str(i64_val), ~"-9223372036854775808");
+ assert_eq!(i64_val.to_str(), ~"-9223372036854775808");
}
#[test]
diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs
index 1f22343ad9c..6fba8a6dd13 100644
--- a/src/libstd/num/strconv.rs
+++ b/src/libstd/num/strconv.rs
@@ -708,14 +708,14 @@ mod test {
mod bench {
use extra::test::BenchHarness;
use rand::{XorShiftRng,RngUtil};
- use uint;
use float;
+ use to_str::ToStr;
#[bench]
fn uint_to_str_rand(bh: &mut BenchHarness) {
let mut rng = XorShiftRng::new();
do bh.iter {
- uint::to_str(rng.gen());
+ rng.gen::().to_str();
}
}
diff --git a/src/libstd/num/uint_macros.rs b/src/libstd/num/uint_macros.rs
index 524b035c9f3..2bf41f4103d 100644
--- a/src/libstd/num/uint_macros.rs
+++ b/src/libstd/num/uint_macros.rs
@@ -380,35 +380,25 @@ pub fn to_str_bytes(n: $T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
f(buf.slice(0, cur))
}
-/// Convert to a string in base 10.
-#[inline]
-pub fn to_str(num: $T) -> ~str {
- to_str_radix(num, 10u)
-}
-
-/// Convert to a string in a given base.
-#[inline]
-pub fn to_str_radix(num: $T, radix: uint) -> ~str {
- let mut buf = ~[];
- do strconv::int_to_str_bytes_common(num, radix, strconv::SignNone) |i| {
- buf.push(i);
- }
- // We know we generated valid utf-8, so we don't need to go through that
- // check.
- unsafe { str::raw::from_bytes_owned(buf) }
-}
-
impl ToStr for $T {
+ /// Convert to a string in base 10.
#[inline]
fn to_str(&self) -> ~str {
- to_str(*self)
+ self.to_str_radix(10u)
}
}
impl ToStrRadix for $T {
+ /// Convert to a string in a given base.
#[inline]
fn to_str_radix(&self, radix: uint) -> ~str {
- to_str_radix(*self, radix)
+ let mut buf = ~[];
+ do strconv::int_to_str_bytes_common(*self, radix, strconv::SignNone) |i| {
+ buf.push(i);
+ }
+ // We know we generated valid utf-8, so we don't need to go through that
+ // check.
+ unsafe { str::raw::from_bytes_owned(buf) }
}
}
@@ -451,7 +441,6 @@ mod tests {
use u32;
use u64;
use u8;
- use uint;
#[test]
fn test_num() {
@@ -536,13 +525,13 @@ mod tests {
#[test]
pub fn test_to_str() {
- assert_eq!(to_str_radix(0 as $T, 10u), ~"0");
- assert_eq!(to_str_radix(1 as $T, 10u), ~"1");
- assert_eq!(to_str_radix(2 as $T, 10u), ~"2");
- assert_eq!(to_str_radix(11 as $T, 10u), ~"11");
- assert_eq!(to_str_radix(11 as $T, 16u), ~"b");
- assert_eq!(to_str_radix(255 as $T, 16u), ~"ff");
- assert_eq!(to_str_radix(0xff as $T, 10u), ~"255");
+ assert_eq!((0 as $T).to_str_radix(10u), ~"0");
+ assert_eq!((1 as $T).to_str_radix(10u), ~"1");
+ assert_eq!((2 as $T).to_str_radix(10u), ~"2");
+ assert_eq!((11 as $T).to_str_radix(10u), ~"11");
+ assert_eq!((11 as $T).to_str_radix(16u), ~"b");
+ assert_eq!((255 as $T).to_str_radix(16u), ~"ff");
+ assert_eq!((0xff as $T).to_str_radix(10u), ~"255");
}
#[test]
@@ -575,28 +564,28 @@ mod tests {
#[test]
fn test_uint_to_str_overflow() {
let mut u8_val: u8 = 255_u8;
- assert_eq!(u8::to_str(u8_val), ~"255");
+ assert_eq!(u8_val.to_str(), ~"255");
u8_val += 1 as u8;
- assert_eq!(u8::to_str(u8_val), ~"0");
+ assert_eq!(u8_val.to_str(), ~"0");
let mut u16_val: u16 = 65_535_u16;
- assert_eq!(u16::to_str(u16_val), ~"65535");
+ assert_eq!(u16_val.to_str(), ~"65535");
u16_val += 1 as u16;
- assert_eq!(u16::to_str(u16_val), ~"0");
+ assert_eq!(u16_val.to_str(), ~"0");
let mut u32_val: u32 = 4_294_967_295_u32;
- assert_eq!(u32::to_str(u32_val), ~"4294967295");
+ assert_eq!(u32_val.to_str(), ~"4294967295");
u32_val += 1 as u32;
- assert_eq!(u32::to_str(u32_val), ~"0");
+ assert_eq!(u32_val.to_str(), ~"0");
let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
- assert_eq!(u64::to_str(u64_val), ~"18446744073709551615");
+ assert_eq!(u64_val.to_str(), ~"18446744073709551615");
u64_val += 1 as u64;
- assert_eq!(u64::to_str(u64_val), ~"0");
+ assert_eq!(u64_val.to_str(), ~"0");
}
#[test]
@@ -638,14 +627,14 @@ mod tests {
#[should_fail]
#[ignore(cfg(windows))]
pub fn to_str_radix1() {
- uint::to_str_radix(100u, 1u);
+ 100u.to_str_radix(1u);
}
#[test]
#[should_fail]
#[ignore(cfg(windows))]
pub fn to_str_radix37() {
- uint::to_str_radix(100u, 37u);
+ 100u.to_str_radix(37u);
}
#[test]
diff --git a/src/libstd/option.rs b/src/libstd/option.rs
index af4fabef78b..7faa98c2433 100644
--- a/src/libstd/option.rs
+++ b/src/libstd/option.rs
@@ -388,6 +388,7 @@ impl Zero for Option {
}
/// An iterator that yields either one or zero elements
+#[deriving(Clone, DeepClone)]
pub struct OptionIterator {
priv opt: Option
}
diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs
index 63f73002009..e91c78e8223 100644
--- a/src/libstd/prelude.rs
+++ b/src/libstd/prelude.rs
@@ -50,15 +50,15 @@ pub use char::Char;
pub use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
pub use hash::Hash;
pub use iter::Times;
-pub use iterator::Extendable;
-pub use iterator::{Iterator, DoubleEndedIterator};
-pub use iterator::{ClonableIterator, OrdIterator};
+pub use iterator::{FromIterator, Extendable};
+pub use iterator::{Iterator, DoubleEndedIterator, RandomAccessIterator, ClonableIterator};
+pub use iterator::{OrdIterator, MutableDoubleEndedIterator};
pub use num::{Num, NumCast, CheckedAdd, CheckedSub, CheckedMul};
pub use num::{Orderable, Signed, Unsigned, Round};
pub use num::{Algebraic, Trigonometric, Exponential, Hyperbolic};
pub use num::{Integer, Fractional, Real, RealExt};
pub use num::{Bitwise, BitCount, Bounded};
-pub use num::{Primitive, Int, Float};
+pub use num::{Primitive, Int, Float, ToStrRadix};
pub use path::GenericPath;
pub use path::Path;
pub use path::PosixPath;
diff --git a/src/libstd/unstable/extfmt.rs b/src/libstd/unstable/extfmt.rs
index 7b1f0e8ced8..83c12f0af5e 100644
--- a/src/libstd/unstable/extfmt.rs
+++ b/src/libstd/unstable/extfmt.rs
@@ -480,7 +480,6 @@ pub mod rt {
use str;
use sys;
use num;
- use uint;
use vec;
use option::{Some, None, Option};
@@ -593,7 +592,7 @@ pub mod rt {
return if prec == 0u && num == 0u {
~""
} else {
- let s = uint::to_str_radix(num, radix);
+ let s = num.to_str_radix(radix);
let len = s.char_len();
if len < prec {
let diff = prec - len;
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 7c8046a64b2..0427eec2b05 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -1161,6 +1161,7 @@ pub trait OwnedVector {
fn reserve(&mut self, n: uint);
fn reserve_at_least(&mut self, n: uint);
fn capacity(&self) -> uint;
+ fn shrink_to_fit(&mut self);
fn push(&mut self, t: T);
unsafe fn push_fast(&mut self, t: T);
@@ -1254,6 +1255,7 @@ impl OwnedVector for ~[T] {
*
* * n - The number of elements to reserve space for
*/
+ #[inline]
fn reserve_at_least(&mut self, n: uint) {
self.reserve(uint::next_power_of_two(n));
}
@@ -1272,6 +1274,17 @@ impl OwnedVector for ~[T] {
}
}
+ /// Shrink the capacity of the vector to match the length
+ fn shrink_to_fit(&mut self) {
+ unsafe {
+ let ptr: *mut *mut Vec<()> = cast::transmute(self);
+ let alloc = (**ptr).fill;
+ let size = alloc + sys::size_of::>();
+ *ptr = realloc_raw(*ptr as *mut c_void, size) as *mut Vec<()>;
+ (**ptr).alloc = alloc;
+ }
+ }
+
/// Append an element to a vector
#[inline]
fn push(&mut self, t: T) {
@@ -2327,6 +2340,7 @@ mod tests {
use sys;
use vec::*;
use cmp::*;
+ use prelude::*;
fn square(n: uint) -> uint { n * n }
@@ -3600,6 +3614,18 @@ mod tests {
}
assert!(cnt == 3);
}
+
+ #[test]
+ fn test_shrink_to_fit() {
+ let mut xs = ~[0, 1, 2, 3];
+ for i in range(4, 100) {
+ xs.push(i)
+ }
+ assert_eq!(xs.capacity(), 128);
+ xs.shrink_to_fit();
+ assert_eq!(xs.capacity(), 100);
+ assert_eq!(xs, range(0, 100).to_owned_vec());
+ }
}
#[cfg(test)]
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 6a3d829aca0..3bad1ed9384 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -28,7 +28,6 @@ use print::pp;
use print::pprust;
use std::io;
-use std::u64;
// The @ps is stored here to prevent recursive type.
pub enum ann_node<'self> {
@@ -2035,24 +2034,24 @@ pub fn print_literal(s: @ps, lit: &ast::lit) {
ast::lit_int(i, t) => {
if i < 0_i64 {
word(s.s,
- ~"-" + u64::to_str_radix(-i as u64, 10u)
+ ~"-" + (-i as u64).to_str_radix(10u)
+ ast_util::int_ty_to_str(t));
} else {
word(s.s,
- u64::to_str_radix(i as u64, 10u)
+ (i as u64).to_str_radix(10u)
+ ast_util::int_ty_to_str(t));
}
}
ast::lit_uint(u, t) => {
word(s.s,
- u64::to_str_radix(u, 10u)
+ u.to_str_radix(10u)
+ ast_util::uint_ty_to_str(t));
}
ast::lit_int_unsuffixed(i) => {
if i < 0_i64 {
- word(s.s, ~"-" + u64::to_str_radix(-i as u64, 10u));
+ word(s.s, ~"-" + (-i as u64).to_str_radix(10u));
} else {
- word(s.s, u64::to_str_radix(i as u64, 10u));
+ word(s.s, (i as u64).to_str_radix(10u));
}
}
ast::lit_float(f, t) => {
diff --git a/src/test/bench/core-set.rs b/src/test/bench/core-set.rs
index 70fe6f706f7..22622c1cac3 100644
--- a/src/test/bench/core-set.rs
+++ b/src/test/bench/core-set.rs
@@ -89,13 +89,11 @@ impl Results {
let mut set = f();
do timed(&mut self.sequential_strings) {
for i in range(0u, num_keys) {
- let s = uint::to_str(i);
- set.insert(s);
+ set.insert(i.to_str());
}
for i in range(0u, num_keys) {
- let s = uint::to_str(i);
- assert!(set.contains(&s));
+ assert!(set.contains(&i.to_str()));
}
}
}
@@ -104,7 +102,7 @@ impl Results {
let mut set = f();
do timed(&mut self.random_strings) {
for _ in range(0, num_keys) {
- let s = uint::to_str(rng.next() as uint);
+ let s = (rng.next() as uint).to_str();
set.insert(s);
}
}
@@ -113,11 +111,11 @@ impl Results {
{
let mut set = f();
for i in range(0u, num_keys) {
- set.insert(uint::to_str(i));
+ set.insert(i.to_str());
}
do timed(&mut self.delete_strings) {
for i in range(0u, num_keys) {
- assert!(set.remove(&uint::to_str(i)));
+ assert!(set.remove(&i.to_str()));
}
}
}
diff --git a/src/test/bench/core-uint-to-str.rs b/src/test/bench/core-uint-to-str.rs
index 4a32fda59d8..4869c486e5e 100644
--- a/src/test/bench/core-uint-to-str.rs
+++ b/src/test/bench/core-uint-to-str.rs
@@ -24,7 +24,7 @@ fn main() {
let n = uint::from_str(args[1]).unwrap();
for i in range(0u, n) {
- let x = uint::to_str(i);
+ let x = i.to_str();
info!(x);
}
}
diff --git a/src/test/bench/noise.rs b/src/test/bench/noise.rs
index e5fac512499..6565fc36dd2 100644
--- a/src/test/bench/noise.rs
+++ b/src/test/bench/noise.rs
@@ -25,7 +25,7 @@ fn random_gradient(r: &mut R) -> Vec2 {
fn gradient(orig: Vec2, grad: Vec2, p: Vec2) -> f32 {
let sp = Vec2 {x: p.x - orig.x, y: p.y - orig.y};
- grad.x * sp.x + grad.y + sp.y
+ grad.x * sp.x + grad.y * sp.y
}
struct Noise2DContext {
diff --git a/src/test/bench/shootout-pfib.rs b/src/test/bench/shootout-pfib.rs
index 611b11560e4..b2491e305b2 100644
--- a/src/test/bench/shootout-pfib.rs
+++ b/src/test/bench/shootout-pfib.rs
@@ -125,7 +125,7 @@ fn main() {
let elapsed = stop - start;
out.write_line(fmt!("%d\t%d\t%s", n, fibn,
- u64::to_str(elapsed)));
+ elapsed.to_str()));
}
}
}
diff --git a/src/test/run-pass/monad.rs b/src/test/run-pass/monad.rs
index 4529ebf831f..7dc859e559e 100644
--- a/src/test/run-pass/monad.rs
+++ b/src/test/run-pass/monad.rs
@@ -40,7 +40,7 @@ impl option_monad for Option {
}
fn transform(x: Option) -> Option<~str> {
- x.bind(|n| Some(*n + 1) ).bind(|n| Some(int::to_str(*n)) )
+ x.bind(|n| Some(*n + 1) ).bind(|n| Some(n.to_str()) )
}
pub fn main() {
diff --git a/src/test/run-pass/reflect-visit-data.rs b/src/test/run-pass/reflect-visit-data.rs
index 8ab1bef286c..72bdc2ee0a6 100644
--- a/src/test/run-pass/reflect-visit-data.rs
+++ b/src/test/run-pass/reflect-visit-data.rs
@@ -529,7 +529,7 @@ impl TyVisitor for my_visitor {
}
fn visit_int(&self) -> bool {
do self.get::() |i| {
- self.vals.push(int::to_str(i));
+ self.vals.push(i.to_str());
};
true
}
diff --git a/src/test/run-pass/static-impl.rs b/src/test/run-pass/static-impl.rs
index e2bf525df1a..520b3583195 100644
--- a/src/test/run-pass/static-impl.rs
+++ b/src/test/run-pass/static-impl.rs
@@ -32,7 +32,7 @@ trait uint_utils {
}
impl uint_utils for uint {
- fn str(&self) -> ~str { uint::to_str(*self) }
+ fn str(&self) -> ~str { self.to_str() }
fn multi(&self, f: &fn(uint)) {
let mut c = 0u;
while c < *self { f(c); c += 1u; }
diff --git a/src/test/run-pass/trait-generic.rs b/src/test/run-pass/trait-generic.rs
index 47d9665217c..6916db28e11 100644
--- a/src/test/run-pass/trait-generic.rs
+++ b/src/test/run-pass/trait-generic.rs
@@ -13,16 +13,16 @@
use std::int;
trait to_str {
- fn to_str(&self) -> ~str;
+ fn to_string(&self) -> ~str;
}
impl to_str for int {
- fn to_str(&self) -> ~str { int::to_str(*self) }
+ fn to_string(&self) -> ~str { self.to_str() }
}
impl to_str for ~str {
- fn to_str(&self) -> ~str { self.clone() }
+ fn to_string(&self) -> ~str { self.clone() }
}
impl to_str for () {
- fn to_str(&self) -> ~str { ~"()" }
+ fn to_string(&self) -> ~str { ~"()" }
}
trait map {
@@ -43,7 +43,7 @@ fn foo>(x: T) -> ~[~str] {
x.map(|_e| ~"hi" )
}
fn bar>(x: T) -> ~[~str] {
- x.map(|_e| _e.to_str() )
+ x.map(|_e| _e.to_string() )
}
pub fn main() {
diff --git a/src/test/run-pass/trait-to-str.rs b/src/test/run-pass/trait-to-str.rs
index 8982b35ff33..8ecad8d4fe1 100644
--- a/src/test/run-pass/trait-to-str.rs
+++ b/src/test/run-pass/trait-to-str.rs
@@ -10,35 +10,26 @@
// xfail-fast
-#[no_std];
-
-extern mod std;
-
-use std::str::StrVector;
-use std::vec::ImmutableVector;
-use std::iterator::Iterator;
-use std::int;
-
trait to_str {
- fn to_str(&self) -> ~str;
+ fn to_string(&self) -> ~str;
}
impl to_str for int {
- fn to_str(&self) -> ~str { int::to_str(*self) }
+ fn to_string(&self) -> ~str { self.to_str() }
}
impl to_str for ~[T] {
- fn to_str(&self) -> ~str {
- fmt!("[%s]", self.iter().map(|e| e.to_str()).collect::<~[~str]>().connect(", "))
+ fn to_string(&self) -> ~str {
+ fmt!("[%s]", self.iter().map(|e| e.to_string()).collect::<~[~str]>().connect(", "))
}
}
pub fn main() {
- assert!(1.to_str() == ~"1");
- assert!((~[2, 3, 4]).to_str() == ~"[2, 3, 4]");
+ assert!(1.to_string() == ~"1");
+ assert!((~[2, 3, 4]).to_string() == ~"[2, 3, 4]");
fn indirect(x: T) -> ~str {
- x.to_str() + "!"
+ x.to_string() + "!"
}
assert!(indirect(~[10, 20]) == ~"[10, 20]!");