rust - 为什么这个 rppal I2c 结构没有任何方法?

标签 rust raspberry-pi i2c

我一直在 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/

相关文章:

rust - 删除引用的对象后引用保持有效多长时间?

python - 带有 pygame.mixer 音频的 Raspberry pi 仅产生静态

linux - Docker - 从 docker 容器中重启树莓派主机

linux - 关于 i2c 设备驱动程序的困惑

generics - 在不固定 `Fn` 参数之一的情况下,在结构定义上指定 `Fn` trait bound

multidimensional-array - 如何使用自定义步幅创建 ndarray::ArrayView?

python - 如何通过 I2C 使用 Raspberry pi 从 Arduino 读取数据

c++ - i2c 错误 : ‘i2c_smbus_read_byte_data’ was not declared in this scope

rust - 在不递归的情况下执行修改后的 DFS 时改变树

Linux 上的 C++ : Listening to keyboard input while running as systemd service