~~~
let x: int = 42;
-let y: ~str = x.to_str();
+let y: StrBuf = x.to_str().to_strbuf();
~~~
**String to int**
**Int to string, in non-base-10**
-Use the `format!` syntax extension.
+Use the `format_strbuf!` syntax extension.
~~~
let x: int = 42;
-let y: ~str = format!("{:t}", x); // binary
-let y: ~str = format!("{:o}", x); // octal
-let y: ~str = format!("{:x}", x); // lowercase hexadecimal
-let y: ~str = format!("{:X}", x); // uppercase hexadecimal
+let y: StrBuf = format_strbuf!("{:t}", x); // binary
+let y: StrBuf = format_strbuf!("{:o}", x); // octal
+let y: StrBuf = format_strbuf!("{:x}", x); // lowercase hexadecimal
+let y: StrBuf = format_strbuf!("{:X}", x); // uppercase hexadecimal
~~~
**String to int, in non-base-10**
let y: &str = x.unwrap();
~~~
-To return an Owned String (~str) use the str helper function [`from_utf8_owned`](http://static.rust-lang.org/doc/master/std/str/fn.from_utf8_owned.html).
+To return an Owned String (StrBuf) use the str helper function [`from_utf8_owned`](http://static.rust-lang.org/doc/master/std/str/fn.from_utf8_owned.html).
~~~
use std::str;
-let x: Result<~str,~[u8]> = str::from_utf8_owned(~[104u8,105u8]);
-let y: ~str = x.unwrap();
+let x: Result<StrBuf,~[u8]> =
+ str::from_utf8_owned(~[104u8,105u8]).map(|x| x.to_strbuf());
+let y: StrBuf = x.unwrap();
~~~
To return a [`MaybeOwned`](http://static.rust-lang.org/doc/master/std/str/enum.MaybeOwned.html) use the str helper function [`from_utf8_lossy`](http://static.rust-lang.org/doc/master/std/str/fn.from_utf8_owned.html). This function also replaces non-valid utf-8 sequences with U+FFFD replacement character.
Phantom types are useful for enforcing state at compile time. For example:
~~~
-struct Door<State>(~str);
+struct Door<State>(StrBuf);
struct Open;
struct Closed;
Door::<Open>(name)
}
-let _ = close(Door::<Open>("front".to_owned()));
+let _ = close(Door::<Open>("front".to_strbuf()));
~~~
Attempting to close a closed door is prevented statically:
~~~ {.ignore}
-let _ = close(Door::<Closed>("front".to_owned())); // error: mismatched types: expected `main::Door<main::Open>` but found `main::Door<main::Closed>`
+let _ = close(Door::<Closed>("front".to_strbuf())); // error: mismatched types: expected `main::Door<main::Open>` but found `main::Door<main::Closed>`
~~~
# FFI (Foreign Function Interface)
`foo`.)
* `expr` (an expression. Examples: `2 + 2`; `if true then { 1 } else { 2 }`;
`f(42)`.)
-* `ty` (a type. Examples: `int`, `~[(char, ~str)]`, `&T`.)
+* `ty` (a type. Examples: `int`, `~[(char, StrBuf)]`, `&T`.)
* `pat` (a pattern, usually appearing in a `match` or on the left-hand side of
a declaration. Examples: `Some(t)`; `(17, 'a')`; `_`.)
* `block` (a sequence of actions. Example: `{ log(error, "hi"); return 12; }`)
~~~
extern crate sync;
# fn main() {
-fn stringifier(channel: &sync::DuplexStream<~str, uint>) {
+fn stringifier(channel: &sync::DuplexStream<StrBuf, uint>) {
let mut value: uint;
loop {
value = channel.recv();
- channel.send(value.to_str());
+ channel.send(value.to_str().to_strbuf());
if value == 0 { break; }
}
}
extern crate sync;
# use std::task::spawn;
# use sync::DuplexStream;
-# fn stringifier(channel: &sync::DuplexStream<~str, uint>) {
+# fn stringifier(channel: &sync::DuplexStream<StrBuf, uint>) {
# let mut value: uint;
# loop {
# value = channel.recv();
-# channel.send(value.to_str());
+# channel.send(value.to_str().to_strbuf());
# if value == 0u { break; }
# }
# }
});
from_child.send(22);
-assert!(from_child.recv() == "22".to_owned());
+assert!(from_child.recv().as_slice() == "22");
from_child.send(23);
from_child.send(0);
-assert!(from_child.recv() == "23".to_owned());
-assert!(from_child.recv() == "0".to_owned());
+assert!(from_child.recv().as_slice() == "23");
+assert!(from_child.recv().as_slice() == "0");
# }
~~~
#, fuzzy
#| msgid ""
#| "~~~~ let x: f64 = 4.0; let y: uint = x as uint; assert!(y == 4u); ~~~~"
-msgid "~~~ let x: int = 42; let y: ~str = x.to_str(); ~~~"
+msgid "~~~ let x: int = 42; let y: StrBuf = x.to_str(); ~~~"
msgstr ""
"~~~~\n"
"let x: f64 = 4.0;\n"
#, fuzzy
#| msgid ""
#| "~~~~ let x: f64 = 4.0; let y: uint = x as uint; assert!(y == 4u); ~~~~"
-msgid "let x: int = 42; let y: ~str = x.to_str_radix(16); ~~~"
+msgid "let x: int = 42; let y: StrBuf = x.to_str_radix(16); ~~~"
msgstr ""
"~~~~\n"
"let x: f64 = 4.0;\n"
msgid ""
"~~~~\n"
"trait Printable {\n"
-" fn to_string(&self) -> ~str;\n"
+" fn to_string(&self) -> StrBuf;\n"
"}\n"
msgstr ""
"~~~~ {.ignore}\n"
#| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
msgid ""
"impl Printable for int {\n"
-" fn to_string(&self) -> ~str { self.to_str() }\n"
+" fn to_string(&self) -> StrBuf { self.to_str() }\n"
"}\n"
msgstr ""
"~~~~ {.ignore}\n"
msgid ""
"~~~~\n"
"trait Printable {\n"
-" fn make_string(&self) -> ~str;\n"
+" fn make_string(&self) -> StrBuf;\n"
"}\n"
msgstr ""
"~~~~ {.ignore}\n"
#, fuzzy, no-wrap
#| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
msgid ""
-"impl Printable for ~str {\n"
-" fn make_string(&self) -> ~str {\n"
+"impl Printable for StrBuf {\n"
+" fn make_string(&self) -> StrBuf {\n"
" (*self).clone()\n"
" }\n"
"}\n"
#| msgid ""
#| "Traits may be implemented for specific types with [impls]. An impl that "
#| "implements a trait includes the name of the trait at the start of the "
-#| "definition, as in the following impls of `Printable` for `int` and `~str`."
+#| "definition, as in the following impls of `Printable` for `int` and `StrBuf`."
msgid ""
"Traits may be implemented for specific types with [impls]. An impl for a "
"particular trait gives an implementation of the methods that trait "
"provides. For instance, the following impls of `Printable` for `int` and "
-"`~str` give implementations of the `print` method."
+"`StrBuf` give implementations of the `print` method."
msgstr ""
"[impl][impls] により特定の型にトレイトを実装することができます。トレイトを実"
-"装する impl は、以下の `Printable` の `int` と `~str` に対する実装のように、"
+"装する impl は、以下の `Printable` の `int` と `StrBuf` に対する実装のように、"
"定義の先頭にトレイトの名前を含みます。"
#. type: Plain text
#, fuzzy, no-wrap
#| msgid "~~~~ {.ignore} // main.rs extern crate world; fn main() { println(~\"hello \" + world::explore()); } ~~~~"
msgid ""
-"impl Printable for ~str {\n"
+"impl Printable for StrBuf {\n"
" fn print(&self) { println!(\"{}\", *self) }\n"
"}\n"
msgstr ""
# struct HashMap<K, V>;
# fn f() {
# fn id<T>(t: T) -> T { t }
-type T = HashMap<int,~str>; // Type arguments used in a type expression
+type T = HashMap<int,StrBuf>; // Type arguments used in a type expression
let x = id::<int>(10); // Type arguments used in a call expression
# }
~~~~
~~~~
enum Animal {
- Dog (~str, f64),
- Cat { name: ~str, weight: f64 }
+ Dog (StrBuf, f64),
+ Cat { name: StrBuf, weight: f64 }
}
-let mut a: Animal = Dog("Cocoa".to_owned(), 37.2);
-a = Cat{ name: "Spotty".to_owned(), weight: 2.7 };
+let mut a: Animal = Dog("Cocoa".to_strbuf(), 37.2);
+a = Cat { name: "Spotty".to_strbuf(), weight: 2.7 };
~~~~
In this example, `Cat` is a _struct-like enum variant_,
* `str_eq`
: Compare two strings (`&str`) for equality.
* `uniq_str_eq`
- : Compare two owned strings (`~str`) for equality.
+ : Compare two owned strings (`StrBuf`) for equality.
* `strdup_uniq`
: Return a new unique string
containing a copy of the contents of a unique string.
represented as a vector of 8-bit unsigned bytes holding a sequence of UTF-8 codepoints.
Since `str` is of unknown size, it is not a _first class_ type,
but can only be instantiated through a pointer type,
-such as `&str` or `~str`.
+such as `&str` or `StrBuf`.
### Tuple types
~~~~
trait Printable {
- fn to_string(&self) -> ~str;
+ fn to_string(&self) -> StrBuf;
}
impl Printable for int {
- fn to_string(&self) -> ~str { self.to_str() }
+ fn to_string(&self) -> StrBuf { self.to_str().to_strbuf() }
}
fn print(a: Box<Printable>) {
~~~~
trait Printable {
- fn make_string(&self) -> ~str;
+ fn make_string(&self) -> StrBuf;
}
-impl Printable for ~str {
- fn make_string(&self) -> ~str {
+impl Printable for StrBuf {
+ fn make_string(&self) -> StrBuf {
(*self).clone()
}
}
~~~~
-`self` refers to the value of type `~str` that is the receiver for a
+`self` refers to the value of type `StrBuf` that is the receiver for a
call to the method `make_string`.
## Type kinds
pub struct Widget {
/// All widgets have a purpose (this is a doc comment, and will show up
/// the field's documentation).
- purpose: ~str,
+ purpose: StrBuf,
/// Humans are not allowed to understand some widgets
understandable: bool
}
Traits may be implemented for specific types with [impls]. An impl for
a particular trait gives an implementation of the methods that
trait provides. For instance, the following impls of
-`Printable` for `int` and `~str` give implementations of the `print`
+`Printable` for `int` and `StrBuf` give implementations of the `print`
method.
[impls]: #methods
fn print(&self) { println!("{:?}", *self) }
}
-impl Printable for ~str {
+impl Printable for StrBuf {
fn print(&self) { println!("{}", *self) }
}
# 1.print();
-# ("foo".to_owned()).print();
+# ("foo".to_strbuf()).print();
~~~~
Methods defined in an impl for a trait may be called just like
impl Printable for int {}
-impl Printable for ~str {
+impl Printable for StrBuf {
fn print(&self) { println!("{}", *self) }
}
impl Printable for f32 {}
# 1.print();
-# ("foo".to_owned()).print();
+# ("foo".to_strbuf()).print();
# true.print();
# 3.14159.print();
~~~~
methods can save a great deal of boilerplate code from having to be
written in impls. Of course, individual impls can still override the
default method for `print`, as is being done above in the impl for
-`~str`.
+`StrBuf`.
## Type-parameterized traits