use pcap::Device;
use pcap::Packet;
fn main() {
let mut cap = Device::lookup().unwrap().open().unwrap();
let mut packets : Vec<Packet> = Vec::new();
while let Ok(packet) = cap.next() {
println!("received packet! {:?}", packet);
packets.push(packet);
}
}
and get this error at compile time:
error[E0499]: cannot borrow `cap` as mutable more than once at a time
--> src/main.rs:9:28
|
9 | while let Ok(packet) = cap.next() {
| ^^^ mutable borrow starts here in previous iteration of loop
This lifetime on this type means that it contains a reference into some other value. In your case, this is a reference into the cap variable, which is where the actual data is stored. Repeated calls to next will overwrite that buffer, so you can't keep several packets alive simultaneously.
You should either handle each packet fully before asking for the next packet, or copy the data into a different buffer.
thanks Alice, I understand it now. So I better have my own data type with its own buffer and header into which I can copy the values from Packet structure, because a Packet variable does not have its own buffer it only has a reference to another buffer in cap variable, which will be overwritten with the next "next()" call.