Stringbuf library


Hi everybody!

Recently I’ve come across some complaints about string appending being difficult to do.
Even before all that, I have often wished to be able to do string concatenation inline within some other expression.

So I decided to do something about it, and write a library.
I have just uploaded the first version of stringbuf, it’s small, efficient and easy to use. And oh yeah you can find the crate here.

Feel free to check it out, there is a code example on the project’s github page.
Contributions are also welcome!


What’s the difference between StringBuf and std::collections:: String, besides the name?


Ah, apologies for not clarifying that better.

StringBuf wraps a String actually, and uses its append_str method to provide a more ergonomic API for concatenation.
Something like “inline appending” (i.e. appending within some larger expression) becomes possible with this. String does have an Add impl for &str, but StringBuf goes a step further and has an Add impl for an S: Into<&str>, adding a little bit in the ergonomics department.
There is also an append() method that allows for fluent-style appending, much as in Java.

I’ll be the first to say this is something that could easily be subsumed by the standard library (I don’t know how strong the arguments for it are), but as a first effort a new crate seems appropriate.


It is unusual to have Into<SomeBorrowedType>. This is more likely the domain of AsRef or Borrow.


Thanks for the feedback!

I’m perfectly willing to change the API if it means that the ergonomics will improve.

But even though I’ve seen AsRef used in stdlib I’m not quite sure what it’s supposed to do.
Sadly neither the docstring nor the Book page it refers me to is very helpful in understanding it’s purpose, though they are wonderful assets in terms of how to use the code. If you could explain the purpose of the trait to me, that would be very welcome.

I wasn’t until just now aware of the Borrow trait, I’ll have to investigate it, and may change my crate depending on what I find.

Update: It seems like AsRef is useful when you want to be able to accept both some owned type O as well as &O, and then end up using it as an &O. Is that correct?


I think so. To me, it seems like a “weaker” Borrow because Borrow allows (but doesn’t require) to borrow in different form such as borrowing a String as a slice (and also requires that hash/eq are compatible with the original type).


Now that I’m off my phone and can more easily test things, I have the following to point out:

Here is how you concatenate a String with a String in today’s rust:

a + &b

Here is how you concatenate a String with a Cow<str>

a + &b

Here is how you concatenate a String with an Rc<String>

a + &b

And while you don’t have to write it like this, here is one way of concatenating a String with an &str:

a + &b

These are all thanks to automatic deref coercions; rust will freely substitute an expression b with e.g. &*b or &**b or &***b and so on if doing so will make things type-check. Okay that description is not entirely right. Ignore me and read the docs. :stuck_out_tongue:

I figure this recent discussion on internals deserves a link. And there’s probably been many, many other discussions like it; string concatenation is kind of a low-hanging fruit.