5 ms·
On the reddit discussion of this [0], someone mentioned using a type of fn parse(&self, input: &mut &str) -> Option<Output> instead of the article's fn parse
by intertextuality 7y ago
On the reddit discussion of this [0], someone mentioned using a type of
fn parse(&self, input: &mut &str) -> Option<Output>
instead of the article's
fn parse(&self, input: &'a str) ->
Result<(&'a str, Output), &'a str>
for composability. I found the article fascinating and plan on going back to see what an xml parsing implementation based on the former would act like.
[0]: https://www.reddit.com/r/rust/comments/bepi63/learning_parser_combinators_with_rust/ https://www.reddit.com/r/rust/comments/bepi63/learning_parse...
- steveklabnik 7y agoThis might be the first time I’ve seen a good use for &mut &T, very cool! For those of you not well-versed in Rust, this is a mutable pointer to an immutable string. This means that you can change the part of the string you’re pointing at, but you can’t change the underlying string itself.
- e12e 7y agoWhat does that mean? Is it equivalent to the pointer starting out (say) pointing to the first letter of the string, but being able to "walk"/iterate along the length of the string?
- bluejekyll 7y agoIt means you can change the string to which the pointer is currently associated. Edit: I removed my initial “no.”
- progval 7y agoBut in this case, it will be used the way that e12e described.
- bluejekyll 7y agoAh. I may have misunderstood what they were suggesting.
- steveklabnik 7y agoEDIT: adjusted the diagram to make it more clear that we're not mutating the &str Here's some string data, in memory somewhere: Hello world A `&str` is a pointer, length combo. The pointer points to the start of the string, the length says how many bytes there are: (p, 11) | \ | V Hello world A `&mut T` is a pointer, so a `&mut &str` looks like this: p | V (p, 11) | \ | V Hello world Since it's mutable, this means we can re-assign what it points to, and create a new `&str` to the later part of the string: p \------\ V (p, 11) (p, 5) | | \ | | | V V Hello world Since the inner &str is immutable, we can't change the underling string. Hope that helps!
- tigershark 7y agoSo it’s basically like a Span in C# if I understood correctly?
- steveklabnik 7y agoI am not familiar with the in-memory representation of Slice, but conceptually at least, yes. We call &str a "string slice" and &[T] more generally a "slice". EDIT: apparently you just edited from Slice to Span, reading https://msdn.microsoft.com/en-us/magazine/mt814808.aspx https://msdn.microsoft.com/en-us/magazine/mt814808.aspx > First, Span<T> is a value type containing a ref and a length, defined approximately as follows: Yep, exactly the same as Span<T> then.
- tigershark 7y agoSorry, I wrote Slice instead of Span in the original comment... Slice is the method that returns a Span that is the equivalent of this rust concept as far as I can tell. At some point in the future I’ll need to delve in rust, it’s quite fascinating, but sadly I don’t have much time nowadays :(
- Gibbon1 7y agoYeah I do that in a command line interface module for some firmware I wrote. No heap, very limited memory, solution string functions that understand the concept of white space delimited tokens. You just walk the pointer down the input string without modifying it.