【发布时间】:2021-05-24 12:15:51
【问题描述】:
这里是新手,如果这是一个愚蠢的问题,请道歉。
我想在我的代码中使用wagyu crate 的功能。这个 crate 具有命令行功能,所以我可以从命令行运行代码,但我似乎无法从我自己的代码中引用它。
我尝试了 2 个选项:
- 在调用 crate(带参数的结构)时复制预期的输入“clap”
- 从 crate 中调用特定函数
对于第 2 项我已经尝试过:
use wagyu::cli::ethereum;
fn main() {
let m: String = String::from("sunny story shrimp absent valid today film floor month measure fatigue pet");
// Returns the address of the corresponding mnemonic.
let passphrase = "";
let pathway = "m/44'/60'/0'/0";
let address = ethereum::from_mnemonic(m, passphrase, pathway);
println!("phrase: {:?}", address);
当我尝试构建此代码时,出现以下编译错误:
error[E0425]: cannot find function `from_mnemonic` in module `ethereum`
--> src\main.rs:37:29
|
37 | let address = ethereum::from_mnemonic::<>(s, passphrase, pathway);
| ^^^^^^^^^^^^^ not found in `ethereum`
但我通过检查 ethereum.rs 文件中的代码知道有一个名为“from_mnemonic”的公共函数(在第 88 行定义)。
有人知道为什么我不能调用这个函数吗?或者,有没有一种简单的方法可以在不使用命令行界面的情况下使用具有 clap 依赖项的 crate?
非常感谢。
【问题讨论】:
-
该函数在
impl EthereumWallet中,因此您首先需要创建该结构的一个实例,但您不能这样做,因为该结构是私有的。 -
我想知道将私有类型的关联函数声明为公共是否有任何区别。我目前想不出。
-
谢谢斯文,这正是我所担心的。那么有没有办法模仿命令行界面的输入呢?例如,从命令行运行 crate:
cargo run ethereum import-hd "sunny story shrimp absent valid today film floor month measure fatigue pet"将返回正确的输出。是否可以用我自己的代码参数来模仿 clap 的输入结构? -
我能想到的最简单的解决方案是简单地构建二进制文件并将其称为子进程。
-
参见
std::process:Command示例。
标签: function import rust crate clap