ZhangHanDong / tao-of-rust-codes

《Rust编程之道》随书源码
https://ruststudy.github.io/tao_of_rust_docs/tao_of_rust/
MIT License
1.19k stars 170 forks source link

「第十三章」代码清单13-26编译报错 #191

Closed iduanyingjie closed 5 years ago

iduanyingjie commented 5 years ago

页码与行数


Rust版本

$ rustc -V
rustc 1.35.0-nightly (3de010678 2019-04-11)

错误信息

error[E0605]: non-primitive cast: `std::result::Result<std::ptr::NonNull<u8>, std::alloc::AllocErr>` as `*mut T`
  --> src/main.rs:45:21
   |
45 |             let p = p as *mut T;
   |                     ^^^^^^^^^^^
   |
   = note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
error[E0308]: mismatched types
  --> src/main.rs:56:28
   |
56 |             System.dealloc(p,
   |                            ^ expected struct `std::ptr::NonNull`, found *-ptr
   |
   = note: expected type `std::ptr::NonNull<u8>`
              found type `*mut _`
ZhangHanDong commented 5 years ago

@iduanyingjie 感谢反馈。参考一下在线的示例源码继续学习: https://ruststudy.github.io/tao_of_rust_docs/tao_of_rust/ch13/security_abstract/fn.drop_ck_test.html

代码13-16应该是 Basic usage: dropck - V1案例。如果你是手工编写代码,也最好检查你是否输入正确。随书源码仓库里的代码是没问题的。

书里的源码我再检查一下。

iduanyingjie commented 5 years ago

@ZhangHanDong Sorry,不是列子的问题,代码确实没有问题,是因为我用 clion的时候调用System.alloc这个方法,它默认use std::alloc::Alloc,我看了下例子中用的是use std::alloc::GlobalAlloc

Basic usage: dropck - V1案例,编译时,在edition=2015中会有书中描述的问题,但是在edition=2018中是可以编译通过的

ZhangHanDong commented 5 years ago

@iduanyingjie 嗯,感谢反馈。std::alloc::Alloc现在还是实验性的API。学习阶段,还是不要太依赖IDE了。