ellipsis

Function ellipsis 

Source
pub fn ellipsis(s: &str, length: usize) -> String
Expand description

Truncates a string and appends an ellipsis ("...") if it exceeds the specified length.

This function trims the input string of leading and trailing whitespace. If the trimmed string’s length exceeds the specified length, it truncates the string to length - 3 characters and appends an ellipsis. If the trimmed string is shorter than or equal to length, it returns the trimmed string as is. If either the trimmed string or the specified length is less than 3, it returns "...".

§Arguments

  • s - The input string to potentially truncate.
  • length - The maximum allowed length of the returned string.

§Returns

  • String - The possibly truncated string with an ellipsis appended.

§Examples

use lowdash::ellipsis;

let result = ellipsis("Hello, World!", 10);
assert_eq!(result, "Hello, ...");

let result = ellipsis("Short", 10);
assert_eq!(result, "Short");

let result = ellipsis("ExactLength", 11);
assert_eq!(result, "ExactLength");

let result = ellipsis("  Trimmed  ", 6);
assert_eq!(result, "Tri...");

let result = ellipsis("Hi", 2);
assert_eq!(result, "Hi");