我一直在 raspberry pi 上玩 Rust,希望构建一个咖啡机 Controller 。我很早就遇到了这个问题。 我正在使用 rppal 库,希望为 LCD 屏幕创建 i2c 驱动程序。
error[E0599]: no method named `set_timeout` found for type `std::result::Result<rppal::i2c::I2c, rppal::i2c::Error>` in the current scope
--> src/main.rs:8:6
|
8 | foo.set_timeout(1000);
| ^^^^^^^^^^^
error[E0599]: no method named `set_slave_address` found for type `std::result::Result<rppal::i2c::I2c, rppal::i2c::Error>` in the current scope
--> src/main.rs:9:6
|
9 | foo.set_slave_address(ADDR);
| ^^^^^^
我基本上是在遵循示例,但似乎我无法使用所需的方法创建支柱!感觉我错过了一些简单的东西!
这是代码,内容不多。
use rppal::i2c::I2c;
const ADDR: u16 = 0x27;
fn main() {
println!("hello world");
let mut foo = I2c::new();
foo.set_timeout(1000);
foo.set_slave_address(ADDR);
}
这是图书馆 -> https://github.com/golemparts/rppal 和我试图复制的例子 -> https://github.com/golemparts/rppal/blob/master/examples/i2c_ds3231.rs
非常感谢!!
最佳答案
在您发布的示例中有这一行:
let mut i2c = I2c::new()?;
问号不是偶然的。请注意,在错误中,您在代码中获得了 i2c 的类型:
error[E0599]: no method named `set_timeout` found for type `std::result::Result<rppal::i2c::I2c, rppal::i2c::Error>` in the current scope
--> src/main.rs:8:6
|
8 | foo.set_timeout(1000);
|
它不是 i2c
,而是一个 Result<i2c, Error>
.本例中的问号解压 Result
, 如果这是一个错误,则将其升级到当前函数之外。
请注意,要使其正常工作,您当前的函数还必须返回一个结果类型,因此您应该匹配 main()
示例中给出的函数签名 fn main() -> Result<(), Box<dyn Error>>
关于rust - 为什么这个 rppal I2c 结构没有任何方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56817393/