const VIDEO_SUFFIX:[&str;5] = [".3gp",".av1",".avi",".flv",".mp4"];
let str = "12313.mp4".to_owned();
// 我想使用 ends_with 匹配多个值,怎么做
1
共 2 条评论, 1 页
const VIDEO_SUFFIX:[&str;5] = [".3gp",".av1",".avi",".flv",".mp4"];
let str = "12313.mp4".to_owned();
// 我想使用 ends_with 匹配多个值,怎么做
评论区
写评论eprintln!("{:?}",VIDEO_SUFFIX.iter().any(|e|{str.ends_with(e)}));//任何一个匹配即为true
可以 谢谢
你想要什么结果?是只要有一个匹配就返回true还是要返回它的数组下标,或者匹配的后缀本身?
fn main(){ const VIDEO_SUFFIX:[&str;5] = [".3gp",".av1",".avi",".flv",".mp4"]; let str = "12313.mp4".to_owned(); eprintln!("{:?}",VIDEO_SUFFIX.iter().find(|&&e|{str.ends_with(e)}));//找到具体的元素 eprintln!("{:?}",VIDEO_SUFFIX.iter().position(|e|{str.ends_with(e)}));//找到具体的下标 eprintln!("{:?}",VIDEO_SUFFIX.iter().any(|e|{str.ends_with(e)}));//任何一个匹配即为true eprintln!("{:?}",VIDEO_SUFFIX.iter().all(|e|{str.ends_with(e)}));//任何一个不匹配即为false }