Skip to content

"if let WPARAM(VK_ESCAPE.0 as usize) = wparam"makes a compile error #104996

Closed as not planned
@ghost

Description

I tried this code:

图片

unsafe extern "system" fn window_proc(hwnd:HWND,msg:u32,wparam:WPARAM,lparam:LPARAM) -> LRESULT{
    let mut result = LRESULT(0);
    match msg {
        WM_KEYDOWN =>{
            let _a:u16 = VK_ESCAPE.0; // VK_ESCAPE(u16)
            let _b:WPARAM = WPARAM(1usize); // WPARAM(usize)

            if let WPARAM(VK_ESCAPE.0 as usize) = wparam { //why
                DestroyWindow(hwnd);
            }
        }
        _ => {
            result = DefWindowProcW(hwnd, msg, wparam, lparam);
        }
    }
    return result;
}

I expected to see this happen: explanation

Instead, this happened: explanation

error: expected one of `)`, `,`, `@`, or `|`, found `.`
  --> src\main.rs:33:36
   |
33 |             if let WPARAM(VK_ESCAPE.0 as usize) = wparam {
   |                                    ^
   |                                    |
   |                                    expected one of `)`, `,`, `@`, or `|`
   |                                    help: missing `,`

error: expected identifier, found keyword `as`
  --> src\main.rs:33:39
   |
33 |             if let WPARAM(VK_ESCAPE.0 as usize) = wparam {
   |                                       ^^ expected identifier, found keyword

error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found keyword `as`
  --> src\main.rs:33:39
   |
33 |             if let WPARAM(VK_ESCAPE.0 as usize) = wparam {
   |                                      -^^ expected one of `)`, `,`, `...`, `..=`, `..`, or `|`
   |                                      |
   |                                      help: missing `,`

error: expected one of `)`, `,`, `@`, or `|`, found `usize`
  --> src\main.rs:33:42
   |
33 |             if let WPARAM(VK_ESCAPE.0 as usize) = wparam {
   |                                         -^^^^^ expected one of `)`, `,`, `@`, or `|`
   |                                         |
   |                                         help: missing `,`

warning: unused import: `h`
 --> src\main.rs:2:135
  |
2 | use windows::{Win32::{System::LibraryLoader::GetModuleHandleW, UI::WindowsAndMessaging::{WNDCLASSEXW, DefWindowProcW}}, core::PCWSTR, h};        
  |                                                                                                                                       ^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unreachable expression
  --> src\main.rs:18:21
   |
17 |         cbClsExtra: todo!(),
   |                     ------- any code following this expression is unreachable
18 |         cbWndExtra: todo!(),
   |                     ^^^^^^^ unreachable expression
   |
   = note: `#[warn(unreachable_code)]` on by default
   = note: this warning originates in the macro `todo` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 1 field
     --> src\main.rs:33:27
      |
33    |             if let WPARAM(VK_ESCAPE.0 as usize) = wparam {
      |                           ^^^^^^^^^ ^ ^^ ^^^^^ expected 1 field, found 4
      |
     ::: C:\Users\zzwxh\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\windows-0.43.0\src\Windows\Win32\Foundation\mod.rs:21158:19
      |
21158 | pub struct WPARAM(pub usize);
      |                   --------- tuple struct has 1 field

For more information about this error, try `rustc --explain E0023`.
warning: `learn_dx11` (bin "learn_dx11") generated 2 warnings
error: could not compile `learn_dx11` due to 5 previous errors; 2 warnings emitted

Meta

rustc --version --verbose:

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-pc-windows-msvc
release: 1.65.0
LLVM version: 15.0.0
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions