result-panic/panic #213
Replies: 13 comments 6 replies
-
第2题的没有答案呀,答案复制的原题 |
Beta Was this translation helpful? Give feedback.
-
#[allow(unused_variables)]
fn main() {
assert_eq!("abc".as_bytes(), [97, 98, 99]);
let v = vec![1, 2, 3, 4];
let ele = v[3];
// unwrap may panic when get return a None
let ele = v.get(3).unwrap();
// Sometimes, the compiler is unable to find the overflow errors for you in compile time ,so a panic will occur
let v = production_rate_per_hour(2);
// because of the same reason as above, we have to wrap it in a function to make the panic occur
divide(15, 1)
}
fn divide(x:u8, y:u8) {
println!("{}", x / y)
}
fn production_rate_per_hour(speed: u8) -> f64 {
let cph: u8 = 221;
match speed {
1..=4 => speed as f64 * cph as f64,
5..=8 => speed as f64 * cph as f64 * 0.9,
9..=10 => speed as f64 * cph as f64 * 0.77,
_ => 0 as f64,
}
}
pub fn working_items_per_minute(speed: u8) -> u32 {
(production_rate_per_hour(speed) / 60 as f64) as u32
} $ RUST_BACKTRACE= cargo run |
Beta Was this translation helpful? Give feedback.
-
/*
} fn main() {
} // 修复所有的 panic,让代码工作
} fn divide(x:u8, y:u8) { fn production_rate_per_hour(speed: u16) -> f64 { pub fn working_items_per_minute(speed: u16) -> u32 { /* 填空以打印全部的调用栈提示: 你可以在之前的默认 panic 信息中找到相关线索$ $env:RUST_BACKTRACE=1 ; cargo run |
Beta Was this translation helpful? Give feedback.
-
还是Linux下展开栈回溯信息的命令好记: |
Beta Was this translation helpful? Give feedback.
-
// 修复所有的 panic,让代码工作
fn main() {
assert_eq!("abc".as_bytes(), [97, 98, 99]);
let v = vec![1, 2, 3];
let _ele = v[2];
let _ele = v.get(2).unwrap();
// 大部分时候编译器是可以帮我们提前发现溢出错误,并阻止编译通过。但是也有一些时候,这种溢出问题直到运行期才会出现
let _v = production_rate_per_hour(0);
divide(15, 1);
println!("Success!")
}
fn divide(x:u8, y:u8) {
println!("{}", x / y)
}
fn production_rate_per_hour(speed: u8) -> f64 {
let cph: u8 = 221;
match speed {
1..=4 => (speed * cph) as f64,
5..=8 => (speed * cph) as f64 * 0.9,
9..=10 => (speed * cph) as f64 * 0.77,
_ => 0 as f64,
}
}
pub fn working_items_per_minute(speed: u8) -> u32 {
(production_rate_per_hour(speed) / 60 as f64) as u32
} RUST_BACKTRACE=1 cargo run |
Beta Was this translation helpful? Give feedback.
-
1、终止了 // 填空
fn drink(beverage: &str) {
if beverage == "lemonade" {
println!("Success!");
// 实现下面的代码
panic!("error");
}
println!("Exercise Failed if printing out this line!");
}
fn main() {
drink("lemonade");
println!("Exercise Failed if printing out this line!");
} 2、就是修复 // 修复所有的 panic,让代码工作
fn main() {
assert_eq!("abc".as_bytes(), [97, 98, 99]);
let v = vec![1, 2, 3,4];
let _ele = v[3];
let _ele = v.get(3).unwrap();
// 大部分时候编译器是可以帮我们提前发现溢出错误,并阻止编译通过。但是也有一些时候,这种溢出问题直到运行期才会出现
let _v = production_rate_per_hour(2);
divide(15, 0);
println!("Success!")
}
fn divide(x:u8, y:u8) {
if y!=0{
println!("{}", x / y)
}
}
fn production_rate_per_hour(speed: u8) -> f64 {
let cph: i32 = 221;
match speed {
1..=4 => ((speed as i32) * cph) as f64,
5..=8 => ((speed as i32) * cph) as f64 * 0.9,
9..=10 => ((speed as i32) * cph) as f64 * 0.77,
_ => 0 as f64,
}
}
pub fn working_items_per_minute(speed: u8) -> f64 {
(production_rate_per_hour(speed) / 60 as f64) as f64
} |
Beta Was this translation helpful? Give feedback.
-
类Unix(Bash): RUST_BACKTRACE=1 cargo run Windows:(没有语法高亮加个 > $env:RUST_BACKTRACE=1 ; cargo run |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
Done✅ |
Beta Was this translation helpful? Give feedback.
-
result-panic/panic
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/result-panic/panic.html
Beta Was this translation helpful? Give feedback.
All reactions