Skip to content
Exercise · Borrowing · lesson 10 of 11

Exercise: first word

Write a function fn first_word(s: &str) -> &str that returns the first word of the string s: its part from the start up to the first space. If the string has no space, the first word is the whole string.

  • first_word("hello world") → "hello"
  • first_word("single") → "single"
  • first_word("") → ""

Constraints: the string holds only Latin letters and spaces, and spaces may stand at the start, at the end and several in a row; if the string is empty or starts with a space, the first word is empty.

Do not add extra use lines unless the task says so explicitly.

first_word.rs — rust
rust · 4 lines
Ctrl+Enter run · Esc leave editor