小编Dje*_*ent的帖子

println!() 仅当对象实现 Debug 特征时

我的结构中有方法,它采用泛型类型T。该类型可能实现Debug特征,但我的结构不需要它。
我需要调试我的应用程序,并且能够打印它会非常有用。不幸的是,我不能这样做,因为该类型可能未实现Debug特征。我可以在我的结构定义中指定T: Debug,但我需要在很多地方添加它以便我的代码进行编译。println!()仅当类型实现此特征时才可以调用宏吗?

像这样的东西:

if argument.implements(Debug) {
    println!("{:?}", argument);
}
Run Code Online (Sandbox Code Playgroud)

rust

4
推荐指数
1
解决办法
1084
查看次数

如何在循环中生成异步方法?

我有有一个对象的矢量resolve()的方法,它使用reqwest来查询外部web API。在resolve()每个对象上调用该方法后,我想打印每个请求的结果。

这是我编译和工作的半异步代码(但不是真正异步的):

for mut item in items {
    item.resolve().await;

    item.print_result();
}
Run Code Online (Sandbox Code Playgroud)

我试图用来tokio::join!产生所有异步调用并等待它们完成,但我可能做错了什么:

tokio::join!(items.iter_mut().for_each(|item| item.resolve()));
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

error[E0308]: mismatched types
  --> src\main.rs:25:51
   |
25 |     tokio::join!(items.iter_mut().for_each(|item| item.resolve()));
   |                                                   ^^^^^^^^^^^^^^ expected `()`, found opaque type
   | 
  ::: src\redirect_definition.rs:32:37
   |
32 |     pub async fn resolve(&mut self) {
   |                                     - the `Output` of this `async fn`'s found opaque type
   |
   = note: expected unit type `()`
            found opaque type `impl std::future::Future`
Run Code Online (Sandbox Code Playgroud)

如何一次调用resolve()所有实例的方法?


这段代码反映了答案——现在我正在处理我不太理解的借用检查器错误——我应该用 …

rust async-await rust-tokio

3
推荐指数
1
解决办法
4651
查看次数

在python中使用SSH

我需要使用Python通过SSH连接其他服务器,执行少量命令并将每个命令的结果分配给不同的变量.
最简单的方法是什么?
我已经尝试过SSHController,但我认为,我已经搞砸了一些提示,脚本正在无休止地等待它.
我会非常感激任何例子.

python ssh

2
推荐指数
1
解决办法
4444
查看次数

在python中从串行读取数据

我得到了返回 NMEA 数据的 GPS 模块。当我尝试使用以下代码打印它返回的所有数据时,我得到了这个.

while True:
try:
    rcv = port.read()
    print rcv
Run Code Online (Sandbox Code Playgroud)

然后,我进行了一些修改,可以更清晰地读取 NMEA 数据。它看起来像这样:

port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=10.0)

line = []
print("connected to: " + port.portstr)

while True:
    try:
        rcv = port.read()
    except:
        rcv = ''


    line.append(rcv)

    if rcv == '\n':
        line = "".join(line)        
        print line
        line = []
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样:

$GPGGA,183345.000,5023.3424,N,01857.3817,E,1,7,1.25,313.3,M,42.1,M,,*53

$GPGSA,A,3,09,26,28,08,15,18,17,,,,,,1.52,1.25,0.88*06

$ GPRMC,183345.000,A,5023.3424,N,01857.3817,E,0.40,55.07,050214,,,A*54

$GPVTG,55.07,T,,M,0.40,N,0.74,K,A*0D

$GPGGA,183346.000,5023.3423,N,01857.3817,E,1,7,1.25,313.3,M,42.1,M,,*57

$GPGSA,A,3,09,26,28,08,15,18,17,,,,,,1.52,1.25,0.88*06

问题是有时它会遗漏一些逗号或其他数据,并且 NMEA 解析器读取错误。有没有更好更干净的方法来通过串行读取整个 NMEA 帧?

python gps nmea serial-port

2
推荐指数
1
解决办法
4154
查看次数

在CentOS 7上找不到mysql-community的mysql_config

我需要mysqlclient通过在CentOS服务器上为python3.6 安装模块pip
安装因以下错误而中断:OSError: mysql_config not found
我找到了一种解决方案,可以安装mysql-devel软件包,但不幸的是,它因以下错误而中断:

Error: mariadb101u-config conflicts with mysql-community-server-8.0.4-0.1.rc.el7.x86_64
Error: mariadb101u-libs conflicts with mysql-community-libs-8.0.4-0.1.rc.el7.x86_64
Error: mariadb101u-libs conflicts with mysql-community-libs-compat-8.0.4-0.1.rc.el7.x86_64
Error: mariadb101u-common conflicts with mysql-community-common-8.0.4-0.1.rc.el7.x86_64
Run Code Online (Sandbox Code Playgroud)

我无法删除mysql-community软件包,因为服务器正在基于该数据库运行MySQL数据库。
有什么方法可以解决此问题而又不会删除有冲突的数据包?

mysql python-3.x centos7

2
推荐指数
1
解决办法
2300
查看次数

Rust:参数要求借用持续“static”

我正在做一些使用 Rust 线​​程的练习。我想打印分块字符串,但我无法解决我偶然发现的生命周期问题。这是我的代码:

let input = "some sample string".to_string();

let mut threads = vec![];
for chunk in input.chars().collect::<Vec<char>>().chunks(2) {
    threads.push(thread::spawn({
        move || -> () {
            println!("{:?}", chunk);
        }
    }))
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行它时,出现以下错误:

error[E0716]: temporary value dropped while borrowed
 --> src\main.rs:7:18
  |
7 |     for chunk in input.chars().collect::<Vec<char>>().chunks(2) {
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------
  |                  |                                            |
  |                  |                                            temporary value is freed at the end of this statement
  |                  creates a temporary which is freed while still in use
  |                  argument requires that borrow …
Run Code Online (Sandbox Code Playgroud)

rust

0
推荐指数
1
解决办法
4609
查看次数

标签 统计

rust ×3

python ×2

async-await ×1

centos7 ×1

gps ×1

mysql ×1

nmea ×1

python-3.x ×1

rust-tokio ×1

serial-port ×1

ssh ×1