Strings and Iterators

이번 훈련은 웹 서버의 라우팅 컴포넌트를 구현합니다. 서버는 _요청경로(request path)_와 일치하는 여러개의 _경로 접두사(path prefixes)_로 구성되어 있습니다. 경로 접두사에는 와일드카드문자를 포함할 수 있습니다. 아래 테스트 코드를 참조하세요

In this exercise, you are implementing a routing component of a web server. The server is configured with a number of path prefixes which are matched against request paths. The path prefixes can contain a wildcard character which matches a full segment. See the unit tests below.

아래 코드를 https://play.rust-lang.org/에 복사하고 테스트를 통과해 보시기 바랍니다. 중간 결과값을 Vec에 할당하지 않도록 주의 하시기 바랍니다.

Copy the following code to https://play.rust-lang.org/ and make the tests pass. Try avoiding allocating a Vec for your intermediate results:

#![allow(unused)]
fn main() {
// TODO: remove this when you're done with your implementation.
#![allow(unused_variables, dead_code)]

pub fn prefix_matches(prefix: &str, request_path: &str) -> bool {
    unimplemented!()
}

#[test]
fn test_matches_without_wildcard() {
    assert!(prefix_matches("/v1/publishers", "/v1/publishers"));
    assert!(prefix_matches("/v1/publishers", "/v1/publishers/abc-123"));
    assert!(prefix_matches("/v1/publishers", "/v1/publishers/abc/books"));

    assert!(!prefix_matches("/v1/publishers", "/v1"));
    assert!(!prefix_matches("/v1/publishers", "/v1/publishersBooks"));
    assert!(!prefix_matches("/v1/publishers", "/v1/parent/publishers"));
}

#[test]
fn test_matches_with_wildcard() {
    assert!(prefix_matches(
        "/v1/publishers/*/books",
        "/v1/publishers/foo/books"
    ));
    assert!(prefix_matches(
        "/v1/publishers/*/books",
        "/v1/publishers/bar/books"
    ));
    assert!(prefix_matches(
        "/v1/publishers/*/books",
        "/v1/publishers/foo/books/book1"
    ));

    assert!(!prefix_matches("/v1/publishers/*/books", "/v1/publishers"));
    assert!(!prefix_matches(
        "/v1/publishers/*/books",
        "/v1/publishers/foo/booksByAuthor"
    ));
}
}

역주

  • 단순 파싱인데 제약조건에 주의 하세요. (Vec 사용금지)
  • match 활용인데 개인적으론 좀 낮선 방법이라 익숙해질 필요가 있습니다.
힌트
  • iter 공식 문서 참조
  • solution은 요소를 Option으로 감싸고 마지막은 None으로 두고 match문을 사용하는 로직입니다.