Skip to content

Instantly share code, notes, and snippets.

@tk3
Created November 25, 2017 11:27
Show Gist options
  • Save tk3/6f5816a6f0fca706592d7f7ca6ab4514 to your computer and use it in GitHub Desktop.
Save tk3/6f5816a6f0fca706592d7f7ca6ab4514 to your computer and use it in GitHub Desktop.
str sample - rust
// str - Rust https://doc.rust-lang.org/std/primitive.str.html
fn main() {
test_str();
test_str_contains();
test_str_find();
test_str_splitn();
}
fn test_str() {
assert_eq!("abc".len(), 3);
assert_eq!("aaa" == "aaa", true);
assert_eq!("bbb".eq("bbb"), true);
assert_eq!("bbb".ne("ccc"), true);
}
// str - Rust https://doc.rust-lang.org/std/primitive.str.html#method.contains
fn test_str_contains() {
let s = "Monday";
assert_eq!(s.contains("Mon"), true);
assert_eq!(s.contains("mon"), false);
}
// str - Rust https://doc.rust-lang.org/std/primitive.str.html#method.find
fn test_str_find() {
let s = "Monday";
assert_eq!(s.find("m"), None);
assert_eq!(s.find('o'), Some(1));
assert_eq!(s.find("day"), Some(3));
}
// str - Rust https://doc.rust-lang.org/std/primitive.str.html#method.splitn
fn test_str_splitn() {
let v: Vec<&str> = "This is my pen".splitn(3, ' ').collect();
assert_eq!(v, ["This", "is", "my pen"]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment