Cannot be formatted with the default formatter

Hey,

Trying to get a CAN reader working, using 'socketcan-alt' and storing the data packets in a variable for future calculations.

It works until:

println!("CAN Output: {}", canout);

When it presents me with the following error underneath canout

for<'r> fn(&'r socketcan_alt::DataFrame) -> &'r [u8] {socketcan_alt::DataFrame::data}` cannot be formatted with the default formatter

Do I need to use std::Format or something like that?

Thanks in advance!

Logan
.
.
.
Below is the whole code:

use socketcan_alt::{*,Frame};
use std::ffi::CString;
use std::io::Result;
use std::sync::Arc;
use std::fmt::Display;
use std::thread;
use std::time::Duration;
use structopt::StructOpt;

#[derive(StructOpt)]
struct Opt {
    ifname: String,
}

fn main() -> Result<()> {
    let opt = Opt::from_args();

    let socket = Arc::new(Socket::bind(CString::new(opt.ifname)?)?);
    socket.set_recv_own_msgs(true)?;
    socket.set_fd_frames(true)?;

    {
        let socket = socket.clone();
        thread::spawn(move || -> Result<()> {
            loop {
                let canraw = socket.recv()?;
                let canout = DataFrame::data;
                println!("CAN Output: {}", canout);
            }
        });
    }

That error message means that canout has a type that does not implement the Display trait. I'm not sure though what you expect by printing a function?

Perhaps you means something like...

loop {
    if let Frame::Data(data_frame) = socket.recv()? {
        println!("CAN Output: {:?}", data_frame.data());
    }
}

canout is not the data, it's a reference to a function called data() that belongs to DataFrame type. You've probably meant to call this function instead of taking a reference to its code.

That did the trick! Thank you so much quinedot - such a simple fix when I see it!

So now I've got the values being outputted on the screen, is there any way to 'unwrap' the output so I am left with just the integer values and not enum socketcan_alt::data ?

From the documentation, I can see that there is u8 data available - how can I access this integer data and save it as canrawdata as either an integer or string?

https://docs.rs/socketcan-alt/0.2.2/socketcan_alt/struct.DataFrame.html

Thanks again!

data_frame.data() returns a slice of bytes (&[u8]), so you'll need to convert that into an integer or a String or perhaps a Vec<u8> if it's non-UTF8.

String:

// data = data_frame.data();
if Ok(s) = std::str::from_utf8(data) {
    let my_string = s.to_string();
}

Vec<u8>:

// data = data_frame.data();
let my_vec = data.to_vec();

For integers, it's going to depend on if you have binary data or a textual representation.