Menu
Coddy logo textTech

Recap - Find First Word

Part of the Logic & Flow section of Coddy's Rust journey — lesson 58 of 66.

challenge icon

Challenge

Easy

You will receive a single input containing a sentence with multiple words separated by spaces. Find and print the first word from the sentence using string slicing.

Requirements:

  • Read the input sentence and trim whitespace
  • Find the position of the first space in the sentence
  • Create a string slice from the beginning of the sentence up to (but not including) the first space
  • Print the first word

Hint: You can use the .find() method to locate the first space character. Call it like this: sentence.find(' '), passing a space character as the argument. This method returns an Option that contains the index if a space is found. Use .unwrap_or(sentence.len()) to handle cases where there might be no space (single word), defaulting to the full length of the sentence.

Input:

  • A sentence with one or more words separated by spaces (e.g., Hello World from Rust)

Output:

  • The first word from the sentence

Try it yourself

use std::io::{self, BufRead};

fn main() {
    // Read input
    let stdin = io::stdin();
    let sentence = stdin.lock().lines().next().unwrap().unwrap().trim().to_string();
    
    // TODO: Write your code below
    // Find the position of the first space
    // Create a string slice from the beginning to the first space
    // Store the first word in a variable
    
    // Print the first word
    // println!("{}", first_word);
}

All lessons in Logic & Flow