this works, but gives a weird warning message (tested on rust playground):
fn main() {
let test = Main {
test: 0
};
let testdefault = Test::default().input;
match test {
Main {
test: testdefault
} => print!("success"),
_ => print!("failure")
}
}
#[derive(Default)]
struct Test {
input: u32
}
struct Main {
test: u32
}
output:
warning: unreachable pattern
--> src/main.rs:10:5
|
7 | / Main {
8 | | test: testdefault
9 | | } => print!("success"),
| |_____- matches any value
10 | _ => print!("failure")
| ^ unreachable pattern
|
= note: `#[warn(unreachable_patterns)]` on by default
warning: unused variable: `testdefault`
--> src/main.rs:5:7
|
5 | let testdefault = Test::default().input;
| ^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_testdefault`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `testdefault`
--> src/main.rs:8:13
|
8 | test: testdefault
| ^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_testdefault`
output: success
however, this does not work at all:
fn main() {
let test = Main {
test: 0
};
- let testdefault = Test::default();
match test {
Main {
+ test: testdefault.input
} => print!("success"),
_ => print!("failure")
}
}
#[derive(Default)]
struct Test {
input: u32
}
struct Main {
test: u32
}
giving the error:
error: expected `,`
--> src/main.rs:8:24
|
7 | Main {
| ---- while parsing the fields for this pattern
8 | test: testdefault.input
| ^
firstly, why are struct fields not accessible in match statements, and secondly, why does the working example have a warning of an unused variable despite the variable clearly being used?