Hello Rust forum!
Century: Take the first 2 digit of 4-digit year, and add it 1. For example, 2020 -> 21, 1567 -> 16 ...
I want to write rust code that it finds century of any year.
My Codes
fn main() {
let result = find_century("2014");
println!("{}", result);
}
fn find_century(year: &str) {
if year.len() != 4 {
panic!("Must to be 4");
}
let century: u32 = year.parse().unwrap() + 1;
century;
}
But, rust raises these errors:
Error
Compiling tarih v0.1.0 (/home/emre/Projects/Learning/Rust/tarih)
error[E0277]: `()` doesn't implement `std::fmt::Display`
--> src/main.rs:3:20
|
3 | println!("{}", result);
| ^^^^^^ `()` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `()`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required by `std::fmt::Display::fmt`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0284]: type annotations needed
--> src/main.rs:11:29
|
11 | let century: u32 = year.parse().unwrap() + 1;
| ^^^^^ cannot infer type for type parameter `F` declared on the associated function `parse`
|
= note: cannot satisfy `<_ as std::str::FromStr>::Err == _`
help: consider specifying the type argument in the method call
|
11 | let century: u32 = year.parse::<F>().unwrap() + 1;
| ^^^^^
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0277, E0284.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `tarih`.
How can i solve this problem?