Busca e Teste
Nesta página
Três métodos cobrem a maioria das verificações de formato e filtragem sem precisar de expressões regulares:
.contains(sub)— a substring existe em qualquer posição?.starts_with(prefixo)— começa com esse texto?.ends_with(sufixo)— termina com esse texto?
Todos são case-sensitive: normalize com .lower() antes se precisar de
comparação sem distinção de maiúsculas.
Verificação de prefixo, sufixo e substring; validação de URL e extensão de arquivo.
06-search-and-test.zolo
// Feature: Searching and testing strings
// Syntax: `.contains(sub)`, `.starts_with(p)`, `.ends_with(s)`
// When to use: validate format, filter, decide without regex.
use std::regex
use std::url
let phrase = "the quick brown fox"
// -- contains: does the substring exist? ------------------------
print(phrase.contains("quick"))
// expected: true
print(phrase.contains("cat"))
// expected: false
// -- starts_with / ends_with -----------------------------------
print(phrase.starts_with("the"))
// expected: true
print(phrase.ends_with("fox"))
// expected: true
print(phrase.starts_with("THE"))
// expected: false (case-sensitive)
// -- Practical cases -------------------------------------------
// Validate URL prefix.
fn is_https(url: str) -> bool {
return url.starts_with("https://")
}
print(is_https("https://zolo-lang.devzolo.com")) // true
print(is_https("http://example.com")) // false
// Validate file extension.
fn is_image(name: str) -> bool {
return name.ends_with(".png") || name.ends_with(".jpg")
}
print(is_image("photo.png")) // true
print(is_image("doc.pdf")) // false
// Filter list by substring.
let files = ["main.zolo", "test.rs", "lib.zolo", "build.sh"]
for f in files {
if f.contains(".zolo") {
print(f)
}
}
// expected:
// main.zolo
// lib.zolo
Desafio
Escreva uma função is_zolo_file(name: str) -> bool que retorna true apenas
se o nome termina com .zolo e não começa com _. Teste com "main.zolo",
"_test.zolo" e "lib.rs".
Veja também