#Why does string concatenation take a String and a String reference?

55 messages ยท Page 1 of 1 (latest)

steel rune
#

Is there a reason why the creator of rust decided to make string concatenation work this way?

s3: String = s1 + &s2;

Like, why not simply make it work like this:

s3: String = &s1 + &s2;
// ofc this is invalid but it's a "what if"

Now all 3 strings are valid, the ownership isn't taken away from either s1 or s2. We just get a fresh String constructed from s1 and s2. And now symmetry is also restored back to the addition operator.

dense shell
#

I think this is from the idea that allocations should be explicit.

sick oracle
#

Ownership is taken away from s1

#

ops::Add takes ownership of the first operand and a reference to the second

dense shell
#

It takes ownership of both

steel rune
sick oracle
#

No it doesn't

#

Take ownership of both

steel rune
#

yeah in rust it only takes ownership of the 1st String

neon aspen
#

If both sides are string slices then the implementation has no choice but to allocate a new String to store the result in. With the left hand side being an already allocated String it can just reuse that storage - say you chain multiple adds together, you wouldn't want to allocate a new String each time

dense shell
sick oracle
#

If you read the signature of Add, this is the only sensible way to do it

steel rune
sick oracle
#

The way you gave also reuses the first string's memory

#

That's why ownership of s1 is taken

steel rune
dense shell
#

Then you could never reuse the allocation

sick oracle
#

Rust makes you acknowledge new allocation

steel rune
#

hmm so for operatros all allocation has to be explicit

#

but it's fine if it's not explicit with functions that I write myself

sick oracle
#

It doesn't have to be that way, but that's the way it is by choice

dense shell
#

It's unidiomatic for the basic operators to allocate. There is no requirement by the language here, only style.

steel rune
#

so it's more of a rule of thumb?

dense shell
#

yes

steel rune
#

I see

#

well I'm not planning to change how it works lol

#

but at least now I see the reason behind it

#

thanks for the explanation guys

#

is there a ?solved command here or something similar to mark a topic as closed?

sick oracle
#

๐Ÿคท

steel rune
#

if not that's ok

#

I'm just used to such features from other servers

split wren
# steel rune Is there a reason why the creator of rust decided to make string concatenation w...

I guess if I understand it is because if s1 where to be &s1 then it would be a reference then you know concatenation with a reference is not possible but now you might ask why not s1 + s2 . I think it is because s2 is being borrowed when concatenation occurs to create a new string so what basically happens under the hood is that s1 takes 1byte of space and s2 takes another seperate 1byte of space and now when you concate and assign to s3 then what happens is that it create a new space of 2byte and takes s1 by value which causes a move while s2 is borrowed and basically cloned under the hood which causes it to be reused again and again. To better understand this I have done some playing with your example and here are the errors and the code.

#

As you can see in the above image I able to reuse s3 as it gets borrowed which causes a clone under the hood while I cannot access s1 as it gets moved by value into the new space created.

Here is the error I get if I try to run this:

#

which shows that s1 gets moved by value while s3 gets cloned so that's why to fix this the compile clearly advices to use clone as this will make the reuse of s1 possible afterwards. I hope you got my point ๐Ÿ™‚ . If you have further doubts feel free to ask I would be glad to help ๐Ÿ™‚ .

#

Also Note I have used the word space for abbrevation what I actually mean by space is memory allocation or memory space allocation .

steel rune
# split wren

off-topic but that's a sick bg for your code editor

steel rune
split wren
steel rune
split wren
steel rune
#

cool cool

#

I use vscode, so that theme might not be available for me, but I'll look for alternatives that allow me to change the bg

split wren
steel rune
#

oh that's awesome

#

ty for the link

split wren
# steel rune ty for the link

If you need any help regarding async-await, actix-web or any other rust stuff feel free to ping me here I would be glad to help ๐Ÿ™‚ I hope you have a great day ๐Ÿ™‚ .

steel rune
split wren
#

Also, one thing to note here with actix-web you will need to have some experience with async await in rust. Assuming you know it already then I would be really happy to help you out ๐Ÿ™‚ .

steel rune
#

I have a decent understanding of it, mostly from python, but ik how to find my way around in most situations

#

if I get stuck on a problem that I won't be able to solve then yeah I'll dm you