【发布时间】:2020-10-28 02:19:06
【问题描述】:
在这里学习一些 Rust..
我可以在命令行上做到这一点!
// main.rs ------------
mod attach;
fn main(){
attach::dothings();
}
// attach.rs ------------
use std::{thread, time};
pub fn dothings(){
let mut cnt = 1;
loop {
// !I could blink a led here or read a sensor!
println!("doing things {} times", cnt);
thread::sleep(time::Duration::from_secs(5));
cnt = cnt +1;
if cnt == 5 {break;}
}
}
但是在做嵌入的时候,main 什么都不能返回
fn main() -> ! {...}
使用类似代码时!我得到了这个错误。 (?隐式返回是()?)
| -------- implicitly returns `()` as its body has no tail or `return`
9 | fn main() -> ! {
| ^ expected `!`, found `()`
关于如何修复它的任何想法?
【问题讨论】:
-
我很困惑。为什么要尝试使用 never 类型
!,表示函数永远不会返回,当它返回时(大约 20 秒后)? -
!意味着你需要永远循环,如果你想显式循环 5 次,然后这样做并在之后添加一个不同的无限循环。 -
正如我所读到的,当在 MCU 中使用此代码时(没有打印,但读取传感器),它需要有“从不!”关于主要功能!如果我错了,请纠正我。我只想在主函数之外放置一些逻辑(在本例中为循环)。 @loganfsmyth 如果我删除循环计数,情况是一样的!有没有办法让“dothings”函数什么都不返回?