rust - 我如何使用另一个模块的私有(private)特征?

标签 rust

我正在尝试运行以下代码段:

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/

相关文章:

rust - 我如何通过枚举变体 "toggle"?

rust - 如何正确地要求另一个特征中的特征?

arrays - 如何在数组中添加一个值?

random - 如何使用 Rust 中的 rand crate 输入整数种子来生成随机数?

random - 特征 `rand_core::CryptoRng` 未针对 `OsRng` 实现

rust - 如何检查命令行参数的第二个元素?

rust - 如何在 Rust 中运行单个文档测试?

rust - 启用项目功能时启用 Rust nightly 功能

class - 结构成员名称是否比我使用继承的类成员使用更多内存?

rust - 为什么此参数在此Rust函数中正确取消引用?