#Why types like [u8] and str are not sized

61 messages · Page 1 of 1 (latest)

lilac nova
#

I know that they are not sized that's why we use &str [" meaning a adresss + size"] to str but why str is non sized .?? If you can share the internals of memory i will be highly thankful to you

verbal jetty
#

-string

paper palmBOT
#

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.

https://i.redd.it/rpuef1pqf2561.png

verbal jetty
#

that should cover it

#

str is just some continuous region of bytes that can be read as an UTF-8 string

ruby niche
#

the size is known, just not at compile time

lilac nova
verbal jetty
#

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.

sacred marten
lilac nova
#

When the program runs.??

sacred marten
lilac nova
#

.??

sacred marten
#

Sure

#

Post it here

lilac nova
#

At this

#

Ah shit

#

I ve posted wrong vode

#

Code

#

Ah I see

#

What I did was

lilac nova
#

But not actually modified the str

#

Lol

#

As they are in the read-only memory so they can't be

#

Modified

lilac nova
sacred marten
#

String literals are in read-only memory

lilac nova
sacred marten
#

String literals are the things in quotes like "abc"

lilac nova
#

With &str

#

So that we can point it to some else &str

#

.??

sacred marten
#

&str that are created from elsewhere might not be in read-only memory

lilac nova
#

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

sacred marten
#

Do you mean let x: &mut str = "Heyy"; ?

#

That will give a compile error.

lilac nova
#

But its working.??

sacred marten
#

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.

lilac nova
#

Re*

#

Re assignment*

sacred marten
#

Yeah

lilac nova
#

Something like

fn main() {
    let mut s = "Hello Rusty";
    s ="heyy";
    
}
lilac nova
# sacred marten Yeah

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

sacred marten