Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

rust
communitycreator

What is string.ends_with() method in Rust?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

The ends_with() method is used to check if a particular substring ends a particular string. For example, with this method, we can check if the string, "Edpresso", ends with "presso".

Syntax

string.ends_with(subString)
Syntax for ends_with() method in Rust

Parameters

  • string: This is the string we want to check if it ends with another string—the substring.
  • subString: This is the substring that we want to check if it ends with the string, string.

Return value

It returns true if the string ends with the specified substring. Otherwise, it returns false.

Example

fn main() {
// create some strings
let str1 = "Edpresso";
let str2 = "Educative";
let str3 = "Rust";
let str4 = "Educative is the best platform!";
// create strings to check
let end1 = "presso";
let end2 = "cative";
let end3 = "R";
let end4 = "best platform!";
// check if strings ends with the sub-strings
println!(" {} ends with {}: {}",str1, end1, str1.ends_with(end1));
println!(" {} ends with {}: {}",str2, end2, str2.ends_with(end2));
println!(" {} ends with {}: {}",str3, end3, str3.ends_with(end3));
println!(" {} ends with {}: {}",str4, end4, str4.ends_with(end4));
}
Check if a string ends with a specified substring in Rust

Explanation

  • Lines 3–6: We create some strings.
  • Lines 9–12: We create the substrings we want to check if it ends with the strings we created.
  • Lines 15–18: We use the ends_with() method to check if the strings end with the specified substrings and print the results to the console.

RELATED TAGS

rust
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring