Skip to content
Tutorials Center
My learning

Topics

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.

Rust
let line = "-".repeat(10);
let csv = ["a", "b", "c"].join(",");

Try it

Rust repeat, concat and join

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.

Rust Other ways to build a string

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

Rust
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

  • join is not a method of iterators. Iterator has no join in std; collect into a Vec<String> (or Vec<&str>) first, or use fold, or the itertools crate.
  • join and concat allocate once. They compute the total length first, so they are faster than building with repeated +.
  • + consumes its left side. a + &b moves a and returns it with b appended; a can no longer be used. The right side must be &str (a &String coerces automatically). Chaining + is fine for a few pieces; use format! or push_str for more.
  • += and push_str append in place and are the right tool in loops. Call String::with_capacity or reserve if you know the final size.
  • format! is the clearest way to mix text with numbers or other Display values, at the cost of formatting machinery.
  • write! / writeln! into a String needs use std::fmt::Write. They return a Result, which is always Ok for strings.
  • A pattern with repeat is drawing rules and indentation: " ".repeat(depth * 2).
  • Joining paths with join on &str is not portable; use std::path::Path::join or PathBuf::push.