Expected type, found `"(\\d{3})"`

Hello again,
I'm trying to follow part of the regex documentation by implementing my regex as a static, but, I'm receiving a compile error on the string literal. Please can anybody offer any advise?

The code:

impl Camera {
    fn from_vector(fields: Vec<&str>) -> Camera {
        lazy_static! {
            static ref RE: Regex::new("(\\d{3})").unwrap();
        }
<snip...>

The error message:

expected type, found `"(\\d{3})"`
expected typerustc
main.rs(47, 39): expected type
lib.rs(168, 46): while parsing argument for this `ty` macro fragment

- static ref RE: Regex::new("(\\d{3})").unwrap();
+ static ref RE: Regex = Regex::new("(\\d{3})").unwrap();

As I mentioned in the other thread, using r"(\d{3})" might help improve readability as well. Also I guess you’re not needing the extra group, so the parentheses could be removed, too?

2 Likes

Fantastic thank you, I'm still getting used to the syntax and hadn't noticed my missing type.
The regex is now clearer too, but what does the r"" do? I've not been able to find that in the docs, can you point me towards the right page, please?

Googling quickly for a good explanation of raw string literals, this one seems decent

If you line reading a more "official" (but also way more technical) source, this is the relevant page in the reference

Tokens - The Rust Reference

1 Like

Thank you, having the correct search term helps :slight_smile:

I see, maybe should've used the term “raw string literal” here, too. You could've found it though :wink::

⟶ other thread:

1 Like

You're right, this is what I get for reading posts and trying to find things at 5am when I can't sleep :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.