("res = {}", res); } 上述代码会导致编译时的错误,因为if与else分支产生了不同类型的值。Rust在错误提示信息中指出了程序出现问题的地方: error[E0308]:`if`and`else`have incompatible types --> src\main.rs:10:9|7|letres =ifnum %2==0{ | _______________-8| |123
let number = if condition { 5 } else { "six" }; println!("The value of number is: {}", number); } $ cargo run Compiling branches v0.1.0 (file:///projects/branches) error[E0308]: `if` and `else` have incompatible types --> src/main.rs:4:44 | 4 | let number ...
if 和else 分支的值类型是不相容的,同时 Rust 也准确地指出在程序中的何处发现的这个问题: $ cargo run Compiling branches v0.1.0 (file:///projects/branches) error[E0308]: `if` and `else` have incompatible types --> src/main.rs:4:44 | 4 | let number = if condition { 5 } else { "...
fn main() { let condition = true; let number = if condition { 5 } else { "six" }; println!("The value of number is: {}", number); } 1. 2. 3. 4. 5. 6. 7. 会提示if and else have incompatible type. 循环语句 Rust中循环语句有三种类型,loop,while,for loop fn main() { l...
Rust 编译不通过,报了一个错误,“if 和else 类型不匹配”(if and else have incompatible types)。完整的输出如下: error[E0308]: `if` and `else` have incompatible types --> crates/day-7/syntax/src/main.rs:13:12 | 11 | let message = if apples > 10 { | ___- 12 | | "Lots of app...
fnmain(){lett=true;lets=ift{5}else{"6"};println!("{}",s)} 这种就会报错 ➜ fun git:(master)✗cargorun Compiling fun v0.1.0(/Users/Work/Code/Rust/student/fun)error[E0308]:`if`and`else`have incompatible types -->src/main.rs:6:9|3|lets=ift{|___-4||5||- expected because...
error[E0308]:`if`and`else`have incompatible types-->src/main.rs:6:9|3|/ifforward{4||v.iter()||---expected becauseofthis5||}else{6||v.iter().rev()||^^^expected struct`std::slice::Iter`,found struct`Rev`7||}||___-`if`and`else`have incompatible types|=note:expected...
// error[E0308]: `if` and `else` have incompatible types fn return_animal(condition: bool) -> impl Animal { if condition { Dog { name: String::from("狗") } } else { Cat { name: String::from("猫") } } } 1. 2. 3. ...
I tried this code: Version 1 (Compiles successfully): use std::hint::black_box; use std::any::type_name_of_val; fn foo() {} fn bar() {} fn main() { let x = if black_box(true) { foo } else { bar }; println!("{}", type_name_of_val(&x)); //...
怎么解决?rust中的“operator has incompatible types”错误错误确实令人困惑。问题是lazy_static静态变量是...