Regex_replace! from lazy_regex

I'm trying to use regex_replace! from the lazy-regex crate, getting this:

expected 2 arguments, found 1rust-analyzermismatched-arg-count
expected &str, found Stringrust-analyzertype-mismatch
this function takes 2 arguments but 1 argument was supplied
an argument is missingrustcE0057
lib.rs(124, 60): closure defined here
lib.rs(124, 21): provide the argument: `regex_replace!(r"^[\\/]", self.path(), |_, _| "")(regex_replace!(r"^[\\/]", self.path(), |_, _| ""), /* value */)`

My code:

let r = regex_replace!(r"^[\\/]", self.path(), |_, _| "").to_owned();

What's the output from cargo check? API messages are typically truncated or otherwise mangled.


n.b. I'm not familiar with lazy_regex specifically, but based on the docs I would expect the example regex to have no capture groups and thus expect the closure to take a single argument (though on the surface this seems like the opposite of the error).

Edit: ...on second thought, that's almost surely it, as the generated code is probably trying to call your closure with a single argument.

1 Like

Here:

C:\Users\matheus-pc\Documents\rialight>cargo check
    Checking rialight_filesystem v1.0.0 (C:\Users\matheus-pc\Documents\rialight\api\rialight_filesystem)
warning: unused import: `regex`
 --> api\rialight_pi\src\lib.rs:1:18
  |
1 | use lazy_regex::{regex, regex_is_match};
  |                  ^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused variable: `project_settings`
  --> api\rialight_pi\src\lib.rs:40:9
   |
40 |     let project_settings = read_project_settings("/").unwrap();
   |         ^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_project_settings`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: `rialight_pi` (lib) generated 2 warnings
    Checking hyper v0.14.22
warning: unused import: `PathBuf`
 --> api\rialight_filesystem\src\lib.rs:3:23
  |
3 | use std::path::{Path, PathBuf};
  |                       ^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
   --> api\rialight_filesystem\src\lib.rs:175:33
    |
175 |     fn to_path_object(&self) -> Path {
    |                                 ^^^^ doesn't have a size known at compile-time
    |
    = help: within `Path`, the trait `Sized` is not implemented for `[u8]`
    = note: required because it appears within the type `Path`
    = note: the return type of a function must have a statically known size

error[E0057]: this function takes 2 arguments but 1 argument was supplied
   --> api\rialight_filesystem\src\lib.rs:178:21
    |
178 |             let r = regex_replace!(r"^[\\/]", self.path(), |_, _| "").to_owned();
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ an argument is missing
    |
note: closure defined here
   --> api\rialight_filesystem\src\lib.rs:178:60
    |
178 |             let r = regex_replace!(r"^[\\/]", self.path(), |_, _| "").to_owned();
    |                                                            ^^^^^^
    = note: this error originates in the macro `regex_replace` (in Nightly builds, run with -Z macro-backtrace for more info)
help: provide the argument
    |
178 |             let r = regex_replace!(r"^[\\/]", self.path(), |_, _| "")(regex_replace!(r"^[\\/]", self.path(), |_, _| ""), /* value */).to_owned();
    |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0308]: mismatched types
   --> api\rialight_filesystem\src\lib.rs:178:47
    |
178 |             let r = regex_replace!(r"^[\\/]", self.path(), |_, _| "").to_owned();
    |                     --------------------------^^^^^^^^^^^------------
    |                     |                         |
    |                     |                         expected `&str`, found struct `String`
    |                     |                         help: consider borrowing here: `&self.path()`
    |                     arguments to this function are incorrect
    |
note: associated function defined here
   --> C:\Users\matheus-pc\.cargo\registry\src\github.com-1ecc6299db9ec823\regex-1.7.0\src\re_unicode.rs:534:12
    |
534 |     pub fn replacen<'t, R: Replacer>(
    |            ^^^^^^^^

error[E0308]: mismatched types
   --> api\rialight_filesystem\src\lib.rs:181:13
    |
175 |     fn to_path_object(&self) -> Path {
    |                                 ---- expected `Path` because of return type
...
181 |             ""
    |             ^^ expected struct `Path`, found `&str`

Some errors have detailed explanations: E0057, E0277, E0308.
For more information about an error, try `rustc --explain E0057`.
warning: `rialight_filesystem` (lib) generated 1 warning
error: could not compile `rialight_filesystem` due to 4 previous errors; 1 warning emitted
warning: build failed, waiting for other jobs to finish...

I tried to change the closure to use less or more arguments, but... Oh, strange... the error somewhat faded. I tried different things.

let r = regex_replace!(r"^[\\/]", self.path().as_ref(), |_| "").to_owned().to_string();

I was pretty sure I tried one argument, which makes sense since there's no capture group.

Yeah, that "provide the argument suggestion" is particularly bad, heh.


As a drive-by note, a Path is to a PathBuf like a str is to a String. Path is dynamically sized like a str is, so you'll almost always be dealing with a &Path. So to_path_object should return a &Path.

You can use "some str".as_ref() to turn a &str into a &Path.

1 Like

Thank you! Yeah, I just discovered Path is like str now. I thought Path was boxed like String. I decided to return a String instead of a Path for now.

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.