Skip to content

← Collections, Text and First Iterators step 4 of 21

Easy Primitives

Joining owned strings

Join the supplied strings into one String, inserting exactly one ASCII space between each pair of adjacent entries.

pub fn solve(words: Vec<String>) -> String

Preserve every entry exactly, including empty strings, existing whitespace, newlines and Unicode characters. Do not trim, split or skip entries.

Input Output
["hello", "world"] "hello world"
["one"] "one"
[] ""
["", ""] " "
["", "middle", ""] " middle "
["hello world", "again"] "hello world again"

For a nonempty input of n entries, insert n - 1 separators. An empty vector produces an empty string. There is no extra separator before the first entry or after the last, but empty entries or existing whitespace can still make the result begin or end with spaces.

Use the standard joining operation

The slice join method already implements this behavior. A Vec exposes slice methods through Deref, so the method is available directly on words. Supply the required separator instead of writing separator-placement logic yourself.

The function takes ownership of the vector because that is its required signature. Calling join borrows the entries while constructing the result; accepting owned input does not mean the method reuses the individual strings' buffers. A separate API accepting a borrowed slice could let callers retain their vector, but keep the supplied signature for this exercise.

Replace the starter's todo with the joined result. Passing requires compilation, correct outputs and Clippy with warnings denied. Clippy does not reject every handwritten joining loop; use join here because it directly expresses the task.