lotsoftools

Understanding Rust Error E0748

Introduction to Rust Error E0748

Rust Error E0748 occurs when a raw string literal is not correctly terminated. The number of trailing '#' characters must match the number of leading '#' characters.

Erroneous Code Example

fn main() {
  let dolphins = r##"Dolphins!"#; // error!
}

In the code above, the raw string literal starts with two leading '#' characters (r##) but ends with only one trailing '#' character. This causes an E0748 error as the leading and trailing '#' counts do not match.

Correcting the Error

To fix the E0748 error, ensure that the number of leading and trailing '#' characters are equal.

Correct Code Example

fn main() {
  let dolphins = r#"Dolphins!"#; // One `#` at the beginning, one at the end - all good!
}

In the corrected code above, the raw string literal has an equal number of leading and trailing '#' characters (r# and # respectively), resolving the E0748 error.

Example with Multiple '#' Characters

Raw strings can have multiple '#' characters to include strings with double quotes and escape sequences. The error E0748 still occurs if the number of '#' characters on both sides is not equal.

Erroneous Code with Multiple '#' Characters

fn main() {
  let my_string = r###"This is a \"complex\" string."##; // error!
}

As seen in the example, there are three leading '#' characters (r###) and only two trailing '#' characters. To fix this error, match the number of '#' characters on both sides.

Corrected Code with Multiple '#' Characters

fn main() {
  let my_string = r###"This is a \"complex\" string."###; // Three `#` on both sides - all good!
}

In the corrected example, there are three '#' characters on both sides of the raw string literal, which successfully resolves the E0748 error.