-
Notifications
You must be signed in to change notification settings - Fork 320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lifetime troubles #1341
Comments
Hi, a colleague of mine wanted to understand why this happens (we're still learning Rust) so I gave a look. I belive the problem arises from the drop() of the OwnedRepr of the BaseArray that's called automatically at the end of the scope (see here in data_repr.rs), but I might be misguided here. Note that this happens also without the list, i.e.: struct OneArray<'a, D: ndarray::Dimension> {
data: &'a ndarray::Array<&'a str, D>,
}
fn does_not_work() {
let onearray = ndarray::array![""];
let list = OneArray { data: &onearray };
} So far I didn't manage to find a way to specify lifetimes to make the code compile, other than making the string's lifetime static. I hope someone else with more knowledge of ndarray's internals (and Rust) can provide a detailed explanation of what's going on 😅 |
I suspect the problem is forcing the lifetime of the array reference and that of the string references to be the same, i.e. I think struct OneArray<'a, 'b, D: ndarray::Dimension> {
data: &'a ndarray::Array<&'b str, D>,
}
fn does_work() {
let onearray = ndarray::array![""];
let list = OneArray { data: &onearray };
} should work. (I think that the difference between using |
Thank you, those link were very helpful. Maybe this can be fixed one day when (and if) those features are stabilized, but for now, I'll probably go for two lifetimes. |
Hi! I've been stuck on an issue for a while and can't figure out what's causing it. Not sure if there's an issue in ndarray, Rust or if I'm just missing something.
Playground
I'm trying to create a struct containing a list of arrays, where the array elements are string slices:
Thing is, this same thing is totally doable with other Rust structs, for example if we replace ndarray with Vec:
I don't understand why this happens with ndarray, and I haven't been successful in recreating the issue with any struct other than ndarray, so that is why I'm posting here. Any guidance would be much appreciated!
The text was updated successfully, but these errors were encountered: