📖 Scenario: Imagine you are building a simple text search tool that helps users find if a small word or phrase exists inside a larger text. This is useful in many apps like search engines, text editors, or chat filters.
🎯 Goal: You will create a Rust program that checks if a given pattern (substring) exists inside a larger text string. You will learn how to set up the text and pattern, write the search logic, and print the result.
📋 What You'll Learn
Create a variable for the main text string
Create a variable for the pattern string to search
Write a loop to check if the pattern exists inside the text
Print whether the pattern was found or not
💡 Why This Matters
🌍 Real World
Searching for words or phrases inside larger texts is common in search engines, text editors, and chat filters.
💼 Career
Understanding substring search helps in building efficient search features and text processing tools in software development.
Progress0 / 4 steps
1
Create the main text string
Create a variable called text and set it to the string "hello world from rust".
DSA Rust
Hint
Use let to create a variable and assign the exact string.
2
Create the pattern string to search
Add a variable called pattern and set it to the string "world".
DSA Rust
Hint
Use let to create the pattern variable with the exact string.
3
Check if the pattern exists in the text
Write an if statement that uses text.contains(pattern) to check if pattern is inside text. Inside the if, create a variable found and set it to true. Otherwise, set found to false.
DSA Rust
Hint
Use text.contains(pattern) inside an if to decide the value of found.
4
Print the search result
Write a println! statement to print "Pattern found: " followed by the value of found.
DSA Rust
Hint
Use println!("Pattern found: {}", found); to print the result.
Practice
(1/5)
1. What is the main purpose of substring search algorithms in strings?
easy
A. To find if a smaller string exists inside a larger string
B. To sort characters in a string alphabetically
C. To reverse the order of characters in a string
D. To count the total number of characters in a string
Solution
Step 1: Understand substring search goal
Substring search algorithms check if a smaller string (pattern) appears inside a bigger string (text).
Step 2: Compare with other string operations
Sorting, reversing, and counting characters are different operations unrelated to searching for a substring.
Final Answer:
To find if a smaller string exists inside a larger string -> Option A