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.
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!I did draw the right thing at first, then convinced myself I was wrong and "fixed" it. Tricky!
If we provided a mutable pointer to the &mut T, would the mutable pointer would take ownership of the "Hello world"? If so, would Rust then throw a compiler error because we've created a situation where we can orphan part of "Hello world"?
Correct.
> The new &mut T pointer takes ownership of the &str reference
&T/&mut T never take ownership. It is true that &mut T must be exclusive, so it's sort of like "temporary ownership", but the underlying T won't be destroyed when the reference goes out of scope.
> so the original specified (p,11) would be destroyed/overwritten when changed to (p,5).
The original &str will still live as long as it lives; we're creating a new one, and changing the &mut to point to it, not overwriting the existing one.
> The &mut T cannot edit the underlying string because it the reference it owns is a nonmutable reference. Even if the underlying string, "Hello world" was a mutable str variable, then &mut T could not access it since it was provided and immutable reference &str as its type.
Correct.
> If we provided a mutable pointer to the &mut T, would the mutable pointer would take ownership of the "Hello world"? If so, would Rust then throw a compiler error because we've created a situation where we can orphan part of "Hello world"?
No, &mut T never takes ownership.
Hope that helps! Happy to answer more questions if you've got them.
Is there a binding between the "top" pointer &mut T, and the string which the pointer/length pair points to?
Can we change our pointer to point somewhere else entirely, say a newly allocated string "goodbye, world"?
p1
\------\
V
(p2, 11) (p3, 13)
| \
| \
| \
V V
Hello World Goodbye WorldEDIT: apparently you just edited from Slice to Span, reading 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.
&[T] is like Span<T>, while a &mut &[T] is like a reference to a Span<T>.
Edit: I removed my initial “no.”
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_parse...