Skip to content

Commit e192cd9

Browse files
committed
auto merge of #12194 : WebeWizard/rust/master, r=cmr
2 parents 2ca02ea + bed34ec commit e192cd9

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/doc/complement-cheatsheet.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,42 @@ let y: ~str = x.to_str_radix(16);
3636
Use [`FromStrRadix`](http://static.rust-lang.org/doc/master/std/num/trait.FromStrRadix.html), and its helper function, [`from_str_radix`](http://static.rust-lang.org/doc/master/std/num/fn.from_str_radix.html).
3737

3838
~~~
39-
use std::num::from_str_radix;
39+
use std::num;
4040
41-
let x: Option<i64> = from_str_radix("deadbeef", 16);
41+
let x: Option<i64> = num::from_str_radix("deadbeef", 16);
4242
let y: i64 = x.unwrap();
4343
~~~
4444

45+
**Vector of Bytes to String**
46+
47+
To return a Borrowed String Slice (&str) use the str helper function [`from_utf8`](http://static.rust-lang.org/doc/master/std/str/fn.from_utf8.html).
48+
49+
~~~
50+
use std::str;
51+
52+
let bytes = ~[104u8,105u8];
53+
let x: Option<&str> = str::from_utf8(bytes);
54+
let y: &str = x.unwrap();
55+
~~~
56+
57+
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).
58+
59+
~~~
60+
use std::str;
61+
62+
let x: Option<~str> = str::from_utf8_owned(~[104u8,105u8]);
63+
let y: ~str = x.unwrap();
64+
~~~~
65+
66+
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.
67+
68+
~~~
69+
use std::str;
70+
71+
let x = bytes!(72u8,"ello ",0xF0,0x90,0x80,"World!");
72+
let y = str::from_utf8_lossy(x);
73+
~~~~
74+
4575
# File operations
4676
4777
## How do I read from a file?

0 commit comments

Comments
 (0)