serial-rs icon indicating copy to clipboard operation
serial-rs copied to clipboard

error: use of deprecated `try` macro

Open White-Rabbit-Scientific opened this issue 2 years ago • 1 comments

From the example on the home page: https://github.com/dcuddeback/serial-rs/tree/master/serial

rustc -V --> rustc 1.72.1

   Compiling rust_serial v0.1.0 (C:\Users\r\GitHub\rust_serial)
error: use of deprecated `try` macro
  --> src\main.rs:18:5
   |
18 | /     try!(port.reconfigure(&|settings| {
19 | |         try!(settings.set_baud_rate(serial::Baud9600));
20 | |         settings.set_char_size(serial::Bits8);
21 | |         settings.set_parity(serial::ParityNone);
...  |
24 | |         Ok(())
25 | |     }));
   | |_______^
   |
   = note: in the 2018 edition `try` is a reserved keyword, and the `try!()` macro is deprecated
help: you can use the `?` operator instead

White-Rabbit-Scientific avatar Oct 04 '23 06:10 White-Rabbit-Scientific

extern crate serial;

use std::env;
use std::io;
use std::time::Duration;

use std::io::prelude::*;
use serial::prelude::*;

fn main() {
    for arg in env::args_os().skip(1) {
        let mut port = serial::open(&arg).unwrap();
        interact(&mut port).unwrap();
    }
}

fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> {
    port.reconfigure(&|settings| {
        settings.set_baud_rate(serial::Baud9600)?;
        settings.set_char_size(serial::Bits8);
        settings.set_parity(serial::ParityNone);
        settings.set_stop_bits(serial::Stop1);
        settings.set_flow_control(serial::FlowNone);
        Ok(())
    })?;

    port.set_timeout(Duration::from_millis(1000))?;

    let mut buf: Vec<u8> = (0..255).collect();

    port.write(&buf[..])?;
    port.read(&mut buf[..])?;

    Ok(())
}

Use the ? operator

lolpro11 avatar Dec 16 '23 17:12 lolpro11