comprehensive-rust day2 练习

题目在这里

pub fn prefix_matches(prefix: &str, request_path: &str) -> bool {
    //split by slash and change element into Option and add None at the end.
    let mut prefixs = prefix.split("/").map(|p| Some(p)).chain(std::iter::once(None));
    let mut request_paths = request_path.split("/").map(|p| Some(p)).chain(std::iter::once(None));
    //matching every two element
    for (prefix, path) in prefixs.zip(request_paths) {
        match (prefix, path) {
            (Some(prefix), Some(path)) => {
                if prefix != "*" && prefix != path {
                    return false;
                }
            }
            (Some(_), None) => return false,
            (None, Some(_)) => break,
            (None, None) => break,
        }
    }
    true
}
posted @ 2022-12-25 23:08  johnny_zhao  阅读(34)  评论(0编辑  收藏  举报