Guess the language by the error
Jan. 16th, 2020 12:32 pmWell, in the previous example it was an interesting case - the same map has been used to find a super class and a sub class in an inheritance tree, and Rust basically told us that it is not sure that the two are different objects. (So you can't assume you can modify the sub class without modifying the super class)
Another interesting example of a similar problem:
Here one stream is sliced into sub-streams of known length (body.take(...)) in a loop. Rust is telling us we can't refer to the parent stream after creating one slice.
Another curious problem that occurred several times:
Suppose I want to do something to a value, but choose different value depending on a condition. Like, read from stream A or stream B derived from A. Creating that second value only on one branch requires knowing what lifetime to assign. This is trivial, if the action to be done is on the same branch, but it is not clear how to do that, if I want the common action to be outside.
Like, (sketch):
won't work, because Chain constructed in that branch has a lifetime of that branch, but must be used outside. One obvious (but ugly*) solution is to create a continuation and call it from both branches.
*ugly - I've seen where this ends up, if you follow it through methodically.
Another interesting example of a similar problem:
error[E0382]: borrow of moved value: `body`
--> src/main.rs:194:19
|
189 | let mut body = self.file.as_mut().take(len as u64);
| -------- move occurs because `body` has type `std::io::Take<&mut dyn std::io::BufRead>`, which does not implement the `Copy` trait
...
194 | while body.read(&mut preamble[0..1])? > 0 {
| ^^^^ value borrowed here after move
...
234 | let mut elem_body = body.take(len as u64);
| ---- value moved here, in previous iteration of loopHere one stream is sliced into sub-streams of known length (body.take(...)) in a loop. Rust is telling us we can't refer to the parent stream after creating one slice.
Another curious problem that occurred several times:
Suppose I want to do something to a value, but choose different value depending on a condition. Like, read from stream A or stream B derived from A. Creating that second value only on one branch requires knowing what lifetime to assign. This is trivial, if the action to be done is on the same branch, but it is not clear how to do that, if I want the common action to be outside.
Like, (sketch):
if ... {
body = preamble[0..premable_sz].chain(&mut body);
}
body.read_exactly(...);*ugly - I've seen where this ends up, if you follow it through methodically.
no subject
Date: 2020-01-16 03:52 pm (UTC)Still trying to figure out...