Rust 可变可选输出引脚
我被所有权所束缚;但我无法使函数中的Rust 可变可选输出引脚,rust,Rust,我被所有权所束缚;但我无法使函数中的选项可用。应该怎样 struct Chip { wake_pin: Option<OutputPin>, } impl Chip { pub fn new(wake_pin: Option<Pin>) -> Chip { Chip { wake_pin: wake_pin.map(|pin| pin.into_output()), } }
选项可用。应该怎样
struct Chip {
wake_pin: Option<OutputPin>,
}
impl Chip {
pub fn new(wake_pin: Option<Pin>) -> Chip {
Chip {
wake_pin: wake_pin.map(|pin| pin.into_output()),
}
}
pub fn awake(&self) {
// Fails
if let Some(pin) = self.wake_pin {
pin.set_low();
}
}
}
fn main() {
let wake_pin = Gpio::new()
.expect("Can not init gpio")
.get(255)
.expect("Could not attach to wake pin");
let chip = Chip::new(Some(wake_pin));
}
struct芯片{
wake_pin:选项,
}
impl芯片{
pub fn新(wake_引脚:选项)->芯片{
芯片{
wake_pin:wake_pin.map(| pin | pin.into_output()),
}
}
酒吧fn清醒(和自我){
//失败
如果让一些(pin)=self.wake_pin{
pin.set_low();
}
}
}
fn main(){
让wake_pin=Gpio::new()
.expect(“无法初始化gpio”)
.get(255)
.expect(“无法连接到尾销”);
让chip=chip::new(一些(wake_pin));
}
我正在使用rppal板条箱,编译器在if let Some
区域失败。我试图借用wake\u pin
,获取选项作为参考和其他一些东西,但我不完全理解所有权规则。我相信我复制了您的设置。如果有不正确的地方,请用相关细节编辑您的问题
src/main.rs:
use rppal::gpio::{Gpio, OutputPin, Pin};
struct Chip {
wake_pin: Option<OutputPin>,
}
impl Chip {
pub fn new(wake_pin: Option<Pin>) -> Chip {
Chip {
wake_pin: wake_pin.map(|pin| pin.into_output()),
}
}
pub fn awake(&self) {
// Fails
if let Some(pin) = self.wake_pin {
pin.set_low();
}
}
}
fn main() {
let wake_pin = Gpio::new()
.expect("Can not init gpio")
.get(255)
.expect("Could not attach to wake pin");
let chip = Chip::new(Some(wake_pin));
}
试图编译此文件(使用货物检查或类似工具),我们会收到一个警告和两个错误
warning: unused variable: `chip`
--> src/main.rs:28:9
|
28 | let chip = Chip::new(Some(wake_pin));
| ^^^^ help: consider prefixing with an underscore: `_chip`
|
= note: `#[warn(unused_variables)]` on by default
error[E0507]: cannot move out of `self.wake_pin.0` which is behind a shared reference
--> src/main.rs:16:28
|
16 | if let Some(pin) = self.wake_pin {
| --- ^^^^^^^^^^^^^ help: consider borrowing here: `&self.wake_pin`
| |
| data moved here
| move occurs because `pin` has type `rppal::gpio::pin::OutputPin`, which does not implement the `Copy` trait
error[E0596]: cannot borrow `pin` as mutable, as it is not declared as mutable
--> src/main.rs:17:13
|
16 | if let Some(pin) = self.wake_pin {
| --- help: consider changing this to be mutable: `mut pin`
17 | pin.set_low();
| ^^^ cannot borrow as mutable
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0507, E0596.
For more information about an error, try `rustc --explain E0507`.
error: Could not compile `tmp`.
To learn more, run the command again with --verbose.
由于您以后可能会使用芯片
,我们可以暂时将警告重命名为\u芯片
,以使其静音
let _chip=chip::new(一些(wake_pin))代码>
第一个错误告诉我们无法将pin移出self
,因为我们只是借用self
。如果我们只是借用self背后的数据,那么将其作废是相当粗鲁的。然而,编译器正在告诉我们一个解决方案<代码>帮助:考虑在这里借用:‘自我。WAKEPIN’< /代码>
结果不是很对,但方向是对的
if let Some(pin) = &self.wake_pin {
pin.set_low();
}
现在,它不再具有typeOutputPin
(一个拥有的值),而是具有type&OutputPin
(一个借用的值)
不过,我们仍然会遇到第二个错误(措辞略有不同)。关键是pin.set_low()
需要pin
作为可变引用。现在,我们将self
作为一个不可变的引用(pub fn awake(&self)
)。如果我们要变异self
或它的任何字段,我们需要可变地接受它。这也意味着我们需要确保pin
是可变借用的
pub fn awake(&mut self) {
if let Some(pin) = &mut self.wake_pin {
pin.set_low();
}
}
请分享实际错误。另外,什么是OutputPin,编译代码需要哪些板条箱/使用行?
pub fn awake(&mut self) {
if let Some(pin) = &mut self.wake_pin {
pin.set_low();
}
}