Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第 13 章 13.1.4节 解引用原生指针 #310

Open
Kreedzt opened this issue Nov 30, 2020 · 0 comments
Open

第 13 章 13.1.4节 解引用原生指针 #310

Kreedzt opened this issue Nov 30, 2020 · 0 comments
Labels
第十三章 第十三章

Comments

@Kreedzt
Copy link

Kreedzt commented Nov 30, 2020

代码清单13-10

变量定义 address 超出范围

playground也报错: 链接

#![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

声明为 u64 即可编译通过

@ZhangHanDong ZhangHanDong added the 第十三章 第十三章 label Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
第十三章 第十三章
Projects
None yet
Development

No branches or pull requests

2 participants