use std::ffi::CStr;
//Return error in case there's an error
macro_rules! reer {
(e: $expr, number: $literal) => {
match $e {
Ok(v) => v,
Err(_) => return $number,
}
};
}
fn main() {
let str_slice: &str = unsafe { reer!( CStr::from_ptr("mystring").to_str(), 1) };
}
I get a bunch of errors:
error: no rules expected the token `CStr`
--> src/main.rs:13:47
|
3 | macro_rules! reer {
| ----------------- when calling this macro
...
13 | let str_slice: &str = unsafe { reer!( CStr::from_ptr("mystring").to_str(), 1) };
| ^^^^ no rules expected this token in macro call
warning: unused import: `std::ffi::CStr`
--> src/main.rs:1:5
|
1 | use std::ffi::CStr;
| ^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error: missing fragment specifier
--> src/main.rs:4:14
|
4 | (e: $expr, number: $literal) => {
| ^
|
= note: `#[deny(missing_fragment_specifier)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #40107 <https://github.com/rust-lang/rust/issues/40107>
error: missing fragment specifier
--> src/main.rs:4:24
|
4 | (e: $expr, number: $literal) => {
| ^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #40107 <https://github.com/rust-lang/rust/issues/40107>
warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` due to 3 previous errors; 1 warning emitted
But my e
is followed by $expr
so I don't understand what's happening.