6 ms·
> What is the preferred way to do this in Rust? Is there something better than using indexes? I don't think so. > Rust doesn't appear to have a way to say "gi
by masklinn 6y ago
> What is the preferred way to do this in Rust? Is there something better than using indexes?
I don't think so.
> Rust doesn't appear to have a way to say "give me the location of this &str in that &str" (even though it seems like it would be safe).
'course there is: str::find (https://doc.rust-lang.org/std/primitive.str.html#method.find https://doc.rust-lang.org/std/primitive.str.html#method.find)
It looks a bit abstract because it takes a Pattern rather than an `&str`, but a char, a slice of chars, an &str, or a predicate function, are all patterns.
- couchand 6y agoPresumably the parent wasn't asking about searching for one during slice in another, but rather locating the original offset of a string slice derived from another.
- masklinn 6y agoAh I see, I'd misunderstood. But there's no guarantee that two &str have any sort of overlap, that relationship is not conserved by the language, so it either would be unsafe or would be failible, it'd basically be pointer-twiddling.
- ridiculous_fish 6y agostr::find performs a substring search, which is incorrect if the same token appears more than once. What I want is the literal offset of a string slice in another, aka pointer arithmetic in C: fn derp() -> usize { let src = "xxxxxxx"; let tok = &src[3..4]; let offset = tok.as_ptr() - src.as_ptr(); offset // should be 3 } this should be doable safely; I think it's just an annoying hole in the API.
- steveklabnik 6y agocast both of those as_ptr()s to usize and this does what you want.
- defen 6y agoI know it's just an example, but in case anyone tries to grab the code - it's worth noting that this will panic if you try to slice into the middle of a UTF-8 sequence, and will give byte offsets and not char offsets, so it might not give the answer you expect if src contains multibyte chars before tok. Also I'm fairly new to Rust so it's possible there's some other way to do it that I just don't know about.
- masklinn 6y ago> What I want is the literal offset of a string slice in another, aka pointer arithmetic in C: I see, I'd misunderstood the original, sorry. > this should be doable safely The language doesn't know that there's any relationship between those two strings, so the method would probably need to be fallible, and be more convoluted than this.
- conradludgate 6y agoI think the point is to have a free method of doing so. Since a &str is a pointer and length, it contains all the information already of where it is inside the parent &str. This also covers the case of when the same token is repeated in the text. src = `let abc = "abc"` token = src[4..7] The 4 and 7 right there are the values we want. And if a str is a pointer and length, then those values should be retrievable with simple arithmetic, rather than a string search The only issue is if the str isn't a substring. The signature would need to be either `fn location(&self, &child) -> Option<(usize, usize)>` or `unsafe fn location_unchecked(&self, &child) -> (usize, usize) `