@@ -112,42 +112,42 @@ fn main() {
112
112
}
113
113
```
114
114
115
- Want a string that's not UTF-8? (Remember, ` str ` and ` String ` must be valid UTF-8)
115
+ Want a string that's not UTF-8? (Remember, ` str ` and ` String ` must be valid UTF-8).
116
116
Or maybe you want an array of bytes that's mostly text? Byte strings to the rescue!
117
117
118
118
``` rust, editable
119
119
use std::str;
120
120
121
121
fn main() {
122
- // Note that this is not actually a &str
123
- let bytestring: &[u8; 20 ] = b"this is a bytestring ";
122
+ // Note that this is not actually a ` &str`
123
+ let bytestring: &[u8; 21 ] = b"this is a byte string ";
124
124
125
- // Byte arrays don't have Display so printing them is a bit limited
126
- println!("A bytestring : {:?}", bytestring);
125
+ // Byte arrays don't have the ` Display` trait, so printing them is a bit limited
126
+ println!("A byte string : {:?}", bytestring);
127
127
128
- // Bytestrings can have byte escapes...
128
+ // Byte strings can have byte escapes...
129
129
let escaped = b"\x52\x75\x73\x74 as bytes";
130
130
// ...but no unicode escapes
131
131
// let escaped = b"\u{211D} is not allowed";
132
132
println!("Some escaped bytes: {:?}", escaped);
133
133
134
134
135
- // Raw bytestrings work just like raw strings
135
+ // Raw byte strings work just like raw strings
136
136
let raw_bytestring = br"\u{211D} is not escaped here";
137
137
println!("{:?}", raw_bytestring);
138
138
139
- // Converting a byte array to str can fail
139
+ // Converting a byte array to ` str` can fail
140
140
if let Ok(my_str) = str::from_utf8(raw_bytestring) {
141
141
println!("And the same as text: '{}'", my_str);
142
142
}
143
143
144
- let quotes = br#"You can also use "fancier" formatting, \
144
+ let _quotes = br#"You can also use "fancier" formatting, \
145
145
like with normal raw strings"#;
146
146
147
- // Bytestrings don't have to be UTF-8
147
+ // Byte strings don't have to be UTF-8
148
148
let shift_jis = b"\x82\xe6\x82\xa8\x82\xb1\x82"; // "ようこそ" in SHIFT-JIS
149
149
150
- // But then they can't always be converted to str
150
+ // But then they can't always be converted to ` str`
151
151
match str::from_utf8(shift_jis) {
152
152
Ok(my_str) => println!("Conversion successful: '{}'", my_str),
153
153
Err(e) => println!("Conversion failed: {:?}", e),
0 commit comments