0

I have the following problem:

pub struct Events { pub ts: u64, pub temperature_multiplier: (f64,f64), // (temperature,multiplier) } 

I have a VecDeque of this struct

elements_vec: VecDeque<Events> 

I would like to be able to go through all the elements of VecDeque and compute the the sum of (temperature * multiplier).

What I have tried:

elements_vec.iter().map(|(_, (t,m))| t * m ).sum() 

It returns an error saying "expected Struct Events".

2 Answers 2

1

Events is not a tuple but a struct. You need to go through Destructuring Structs section in rust book.

elements_vec.iter().map(|Events { temperature_multiplier: (t, m), .. }| t * m).sum::<f64>() 
Sign up to request clarification or add additional context in comments.

Comments

0

This is how I understood your question:

let mut elements_vec: VecDeque<Events> = VecDeque::with_capacity(10); elements_vec.push_back(Events { ts: 0x0002, temperature_multiplier: (20.0, 2.0) }); elements_vec.push_back(Events { ts: 0x0003, temperature_multiplier: (30.0, 3.0) }); elements_vec.push_front(Events { ts: 0x0001, temperature_multiplier: (10.0, 1.0) }); println!("{:?}", &elements_vec); let s : f64 = elements_vec.iter().map(|evnt| evnt.temperature_multiplier.0 * evnt.temperature_multiplier.1 ).sum(); println!("s: {}", s); // s: 140 

The answer is in the compiler's message: map() needs struct Events, which we give it as map(|evnt| /* and use it here */). And it works!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.