#Why types like [u8] and str are not sized
61 messages · Page 1 of 1 (latest)
-string
Basically, str just refers to a continuous region of UTF-8 encoded bytes of unknown length, ie it has an unknown size, referred to as unsized. You can use it by having a &str, which is a read-only view of said bytes, containing a pointer to the start as well as the length (it's referred to as a fat pointer as it contains both an address and metadata).
However, since a &str only views some data, it doesn't actually own it, and nothing happens when you drop a &str. If you want to actually change the length of a string, or create a new one from runtime values, etc, you need a String, which is just a Vec<u8> internally. A reference (&) to a String can also be coerced into a &str since it knows the address and length of the internal Vec.
Lastly, string literals such as "foo" aren't placed on the stack or the heap as in some other languages, but are instead embedded into read-only static memory inside the .exe. Thus they are still a &str as they don't own the data and cannot drop it or modify it, but they are &'static which means they can live for as long as they need to.
that should cover it
str is just some continuous region of bytes that can be read as an UTF-8 string
the size is known, just not at compile time
Also what is byte indexing and how it's diffrent from normal one ??
There is no normal indexing for strings, you have to choose between characters and bytes. This is a bit of a misnomer, because a rust char does not map to actual characters you see on the screen, but just to unicode scalars.
In any case byte indexing is just that, you can choose which byte to start on and end on.
Oh I suppose there is no way to do character indexing, heh. Only iteration.
Rust str uses UTF-8. Each char gets turned in 1, 2, 3, or 4 bytes in UTF-8, depending on which char it is.
Can we modify str .??
When the program runs.??
You cannot modify a &str, because it's an immutable reference.
It is possible to modify a &mut str, although this is very rarely useful.
Wait I am sharing one program can you have a look at it
.??
Take a look
At this
Ah shit
I ve posted wrong vode
Code
Ah I see
What I did was
I first converted String -> &str and then -> as bytes and modified them
But not actually modified the str
Lol
As they are in the read-only memory so they can't be
Modified
Did I say it correct.??
String literals are in read-only memory
So what's the purpose of putting mut .??
String literals are the things in quotes like "abc"
&str that are created from elsewhere might not be in read-only memory
Got that 🎀
So what's the purpose of putting mut .?? With &str So that we can point it to some else &str.??
Something like
let mut &str = "Heyyy"
Cause we can't actually push in any byte too
This is invalid syntax
Do you mean let x: &mut str = "Heyy"; ?
That will give a compile error.
That's different
That's a shorter form of:
let mut s: &str = "Hello";
Note that the mut is on the s. It's not &mut str
In this case, it means that s is a reference that can be changed to point to a different location in memory.
So basically this helps in case of assigning.??
Re*
Re assignment*
Yeah
Something like
fn main() {
let mut s = "Hello Rusty";
s ="heyy";
}
Thanks mate actually this doubt was from the day I started learning rust i can now finally sleep
Any tips .?? I am from java background
Not much aware about memory in depth
idk, not really