fn compute(a: u32, b: u32) -> u32 {
// TODO: 컴파일러 오류를 수정하고 테스트가 통과할 수 있도록 아래 줄을 수정
let multiplier: u8 = 4;
a + b * multiplier
}
#[cfg(test)]
mod tests {
use crate::compute;
#[test]
fn case() {
assert_eq!(compute(1, 2), 9);
}
}
러스트는 정적 타입 언어(statically typed language)로, 타입 강제 변환에 엄격하다.
위의 코드에서도 u8 타입의 multiplier 변수를 u32 타입의 b와 곱하려고 시도하는데, 이는 러스트 컴파일러가 강제로 타입을 변환하지 않기 때문에 mismatched types 에러가 발생한다.
따라서 multiplier 변수를 u8이 아닌 u32 타입으로 지정해야 한다.
정답:
fn compute(a: u32, b: u32) -> u32 {
// TODO: change the line below to fix the compiler error and make the tests pass.
let multiplier: u32 = 4;
a + b * multiplier
}
#[cfg(test)]
mod tests {
use crate::compute;
#[test]
fn case() {
assert_eq!(compute(1, 2), 9);
}
}