Unsure why `"\` fails in documentation test?

⚓ rust    📅 2025-06-18    👤 surdeus    👁️ 2      

surdeus

Surely missing something obvious here but couldn't figure it out.

When running cargo test for the following snippet:

/**
 * Returns all lines in the file matching the Query.
 * This function is case insensitive.
 *
 * # Example
    ```rust
        let query = "ducT";
        let contents = "\
Rust: safe, fast, prodUCtive.\
Pick three.";
        assert_eq!(
            vec!["safe, fast, prodUCtive."],
            minigrep::search_insensitive(query, contents)
        );
    ```
*/
pub fn search_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
    let mut result: Vec<&str> = Vec::new();
    let query_lowercase: &str = &query.to_lowercase(); // coerce
    for line in contents.lines() {
        // to_lowercase() returns a new String
        if line.to_lowercase().contains(query_lowercase) {
            result.push(line); // line is the original
        }
    }
    result
}

fails with

running 1 test
test src/lib.rs - search_insensitive (line 67) ... FAILED

failures:

---- src/lib.rs - search_insensitive (line 67) stdout ----
error[E0765]: unterminated double quote string
 --> src/lib.rs:69:20
  |
2 |     let contents = "\
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0765`.
Couldn't compile the test.

failures:
    src/lib.rs - search_insensitive (line 67)

This doesn't fail to compile as actual test, nor makes any trouble in the playground, but for the playground this doesn't quite matter since it runs the code only.

Searched on GitHub but no issues from what I can tell.

6 posts - 4 participants

Read full topic

🏷️ rust_feed