我正在try 基于正在为其编译应用程序的设备来更改应用程序的图标(我正在使用image
crate加载).不幸的是,在try 运行代码时:
let (icon_rgba, icon_width, icon_height) = {
let icon = if cfg!(target_os = "macos") {
include_bytes!("../assets/icon-macos.png")
} else {
include_bytes!("../assets/icon.png")
};
let image = image::load_from_memory(icon)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
我得到以下错误:
error[E0308]: `if` and `else` have incompatible types
--> src/main.rs:6:13
|
3 | let icon = if cfg!(target_os = "macos") {
| ____________________-
4 | | include_bytes!("../assets/icon-macos.png")
| | ------------------------------- expected because of this
5 | | } else {
6 | | include_bytes!("../assets/icon.png")
| | ^^^^^^^^^^^^^^^^^^^^^^^^^ expected an array with a fixed size of 63249 elements, found one with 471 elements
7 | | };
| |_________- `if` and `else` have incompatible types
|
= note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info)
虽然我知道为什么会发生这个错误,但是有没有比下面列出的代码更好的方法来解决这个问题呢?
let (icon_rgba, icon_width, icon_height) = {
if cfg!(target_os = "macos") {
let icon = include_bytes!("../assets/icon-macos.png");
let image = image::load_from_memory(icon)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
} else {
let icon = include_bytes!("../assets/icon.png");
let image = image::load_from_memory(icon)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
};