我正在尝试运行以下代码段:
use std::net::Ipv4Addr;
use std::ffi::CString;
use std::sys_common::AsInner;
fn main() {
let ip: Ipv4Addr = Ipv4Addr::new(127,0,0,1);
println!("{}", ip.as_inner().s_addr);
}
这失败了
test.rs:3:5: 3:29 error: trait `AsInner` is private
test.rs:3 use std::sys_common::AsInner;
^~~~~~~~~~~~~~~~~~~~~~~~
test.rs:7:20: 7:33 error: source trait is private
test.rs:7 println!("{}", ip.as_inner().s_addr);
^~~~~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:25: 2:56 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
test.rs:7:5: 7:42 note: expansion site
error: aborting due to 2 previous errors
我确实看到 AsInner
特性在 http://doc.rust-lang.org/nightly/src/std/net/ip.rs.html#238 中是私有(private)的
我如何使用该特征?
编辑: 我试图将 Ipv4Addr 转换为 int,我想我可以读取底层的 in_addr。但似乎使用八位字节是一个更好的主意。
最佳答案
Vladimir Matveev 说得最好:
You cannot. The whole idea of private things is that they cannot be used outside of the module they are defined.
关于rust - 我如何使用另一个模块的私有(private)特征?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29726730/