d21336ee0a
Being able to index into the bytes of a string encourages poor UTF-8 hygiene. To get a view of `&[u8]` from either a `String` or `&str` slice, use the `as_bytes()` method. Closes #12710. [breaking-change]
27 lines
1.3 KiB
Rust
27 lines
1.3 KiB
Rust
// 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.
|
|
|
|
pub fn main() {
|
|
let v: Vec<int> = vec!(0, 1, 2, 3, 4, 5);
|
|
let s: String = "abcdef".to_string();
|
|
assert_eq!(v.as_slice()[3u], 3);
|
|
assert_eq!(v.as_slice()[3u8], 3); //~ ERROR: mismatched types
|
|
assert_eq!(v.as_slice()[3i8], 3); //~ ERROR: mismatched types
|
|
assert_eq!(v.as_slice()[3u32], 3); //~ ERROR: mismatched types
|
|
assert_eq!(v.as_slice()[3i32], 3); //~ ERROR: mismatched types
|
|
println!("{}", v.as_slice()[3u8]); //~ ERROR: mismatched types
|
|
assert_eq!(s.as_bytes()[3u], 'd' as u8);
|
|
assert_eq!(s.as_bytes()[3u8], 'd' as u8); //~ ERROR: mismatched types
|
|
assert_eq!(s.as_bytes()[3i8], 'd' as u8); //~ ERROR: mismatched types
|
|
assert_eq!(s.as_bytes()[3u32], 'd' as u8); //~ ERROR: mismatched types
|
|
assert_eq!(s.as_bytes()[3i32], 'd' as u8); //~ ERROR: mismatched types
|
|
println!("{}", s.as_bytes()[3u8]); //~ ERROR: mismatched types
|
|
}
|