That's me again !
And I feel stupid to come again, not founding solutions by myself...
I want to learn more about the language, and low level code.
So I have decided to implement SSH protocole following RFC.
But I have another problem...
Let's look at my code (you can get it on GitHub) :
In SshPacket, I have a into_bytes method that consume the instance and return a slice of u8.
And that slice is converted from a vector.
But the problem is that I can not return final_packet because of local owned references.
I have already try to clone the vector, or values gived to the vector, but it was non conclusive.
Here the cargo build error :
error[E0515]: cannot return value referencing local variable `final_packet`
--> src\ssh\ssh_packet.rs:41:9
|
41 | &final_packet[..]
| ^------------^^^^
| ||
| |`final_packet` is borrowed here
| returns a value referencing data owned by the current function
It's never possible to return a borrowed temporary reference to any newly created data. References are for giving permission to access data that already existed before the function call.
Change your function's return type to be Vec and not include any references anywhere (not a single & in the return type). And then keep cloning or calling .to_owned() until it matches that owning type.
But that mean that the caller has the responsability to convert it ?
Is there a way to do a thing like I want ? Or I believe that the consumer will do it in the right way ?
fn main() {
let buf: Vec<u8> = Vec::new();
// You can do this
let _slice: &[u8] = &buf;
// Or this
fn takes_a_slice(_: &[u8]) {}
takes_a_slice(&buf);
}
Because the data in a Vec<T> and in a [T] are stored the same: