Rust Reference ยท Strings and Text
Rust repeat, concat and join for strings
Reference for building Rust strings from pieces: str::repeat, slice concat and join, the + operator, format!, collect and extend, with allocation notes.
Rust gives you several ways to build one string from many pieces, and each fits a different shape of problem. str::repeat(n) makes n copies of a string. concat() glues a slice of strings together, and join(sep) does the same with a separator between the pieces. For one-off mixes of text and values use format!, and for step-by-step building use String::push_str or the + operator.
concat and join are methods on slices ([S] where S is &str or String), so they work on arrays, Vecs and anything that derefs to a slice. The same two methods also join slices of Vecs into one Vec.
let line = "-".repeat(10); let csv = ["a", "b", "c"].join(",");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
fn repeat(&self, n: usize) -> String fn concat(&self) -> <[S] as Concat<str>>::Output fn join<Sep>(&self, sep: Sep) -> <[S] as Join<Sep>>::Output
| Name | Description |
|---|---|
n |
How many copies of the string to produce. 0 gives an empty string. |
sep |
The separator placed between pieces: a &str for string slices, a &T or &[T] for slices of Vec<T>. |
Returns: a new String for repeat, and for concat and join on string slices. On a slice of Vec<T> they return a Vec<T>.
Errors / panics / edge cases: repeat panics if the result length would overflow usize (or the allocation fails). concat and join of an empty slice return an empty string. join never adds a trailing separator.
Usage notes
joinis not a method of iterators.Iteratorhas nojoininstd; collect into aVec<String>(orVec<&str>) first, or usefold, or theitertoolscrate.joinandconcatallocate once. They compute the total length first, so they are faster than building with repeated+.+consumes its left side.a + &bmovesaand returns it withbappended;acan no longer be used. The right side must be&str(a&Stringcoerces automatically). Chaining+is fine for a few pieces; useformat!orpush_strfor more.+=andpush_strappend in place and are the right tool in loops. CallString::with_capacityorreserveif you know the final size.format!is the clearest way to mix text with numbers or otherDisplayvalues, at the cost of formatting machinery.write!/writeln!into aStringneedsuse std::fmt::Write. They return aResult, which is alwaysOkfor strings.- A pattern with
repeatis drawing rules and indentation:" ".repeat(depth * 2). - Joining paths with
joinon&stris not portable; usestd::path::Path::joinorPathBuf::push.