#![allow(unused)]
fn main() {
let mut s = "hello".to_string();
let r1 = &s as *const String;
let r2 = &mut s as *mut String;
assert_eq!(r1, r2);
let address = 0x7fff1d72307d; // error: literal out of range for i32
let r3 = address as *const String;
unsafe {
println!("r1 is: {}", *r1);
println!("r2 is: {}", *r2);
// Segmentation fault
// assert_eq!(*r1, *r3)
}
}
错误信息
error: literal out of range for i32
--> src/main.rs:7:19
|
7 | let address = 0x7fff1d72307d;
| ^^^^^^^^^^^^^^
|
= note: `#[deny(overflowing_literals)]` on by default
= note: the literal `0x7fff1d72307d` (decimal `140733687410813`) does not fit into the type `i32` and will become `494022781i32`
= help: consider using `i64` instead
代码清单13-10
变量定义
address
超出范围playground也报错: [链接](https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0A%20%20%20%20let%20mut%20s%20%3D%20%22hello%22.to_string()%3B%0A%20%20%20%20let%20r1%20%3D%20%26s%20as%20*const%20String%3B%0A%20%20%20%20let%20r2%20%3D%20%26mut%20s%20as%20*mut%20String%3B%0A%20%20%20%20assert_eq!(r1%2C%20r2)%3B%0A%20%20%20%20let%20address%20%3D%200x7fff1d72307d%3B%0A%20%20%20%20let%20r3%20%3D%20address%20as%20*const%20String%3B%0A%20%20%20%20unsafe%20%7B%0A%20%20%20%20%20%20%20%20println!(%22r1%20is%3A%20%7B%7D%22%2C%20*r1)%3B%0A%20%20%20%20%20%20%20println!(%22r2%20is%3A%20%7B%7D%22%2C%20*r2)%3B%0A%20%20%20%20%20%20%20%2F%2F%20Segmentation%20fault%20%20%0A%20%20%20%20%20%20%20%2F%2F%20assert_eq!(*r1%2C%20*r3)%0A%20%20%20%7D%0A%7D)
错误信息
声明为 u64 即可编译通过