Convert from custom type to f32

Hi guys, I'm an absolute noob, just started playing with Rust and I have a basic question.
How to convert between types where I know the conversion is possible ?
In my case I want to convert from fon::chan::Ch32 to f32 and I am getting the error below.
https://docs.rs/fon/0.2.0/fon/chan/struct.Ch32.html

use fon::{chan::Ch32, mono::Mono32, Audio, Stream}; 

How I try to convert:

let y_position= f32::from(y);

Error:

24 |         let y_position= f32::from(y);
   |                         ^^^^^^^^^ the trait `std::convert::From<&fon::sample::Sample1<fon::chan::Ch32>>` is not implemented for `f32`

According to the error, y has type &Sample1<Ch32>.

It looks like you can use the channels method to copy the channel from y:

let chan: Ch32 = y.channels()[0];

Then you can convert that to a float:

let y_position = f32::from(chan);

A note, you can get monospaced content by using code fences (```) surrounding your code / error on new lines (helps with readability and makes people more like to engage as a result)

Oh thank you very much!
And i will sure use Markdown formatting next time :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.