Hi Rustaceans, I am a web developer in Elixir, I just started out my journey with Rust yesterday, and started following the book on doc.rust-lang.
After going through the 3rd chapter the book told...
try building programs to do the following:
- Convert temperatures between Fahrenheit and Celsius.
So, I thought let try writing some code, celsius to Fahrenheit should be simple and I should be done in 15min, but I was wrong it took me an hour to get it working....
After some...
cannot find tuple struct or tuple variant `ok` in this scope
A pinch of...
expected `f32`, found enum `std::result::Result`
Some...
48 | let result: f32 = (input - 32.0) * 5 / 9.0;
^ no implementation for `f32 * {integer}`
and after some more of this and that I finally came up with...
use std::io;
fn main() {
loop {
println!(
"Enter 1 for fahrenheit to celsius conversation and 2 for celsius to fahrenheit converstion"
);
let mut choice = String::new();
io::stdin()
.read_line(&mut choice)
.expect("Failed to read line");
let choice: u32 = match choice.trim().parse() {
Ok(num) => {
if num < 1 || num > 2 {
println!("Incorrect choice try again !");
continue
} else {
num
}
}
Err(_) => {
println!("Incorrect choice try again !");
continue
}
};
let choice_str = if choice == 1 { "celsius" } else { "fahrenheit" };
println!("Enter temperatur in {}", choice_str);
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
let input: f32 = match input.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("Wrong temperature input, try again !");
continue;
}
};
if choice == 1 {
let result: f32 = (input - 32.0) * 5.0 / 9.0;
println!("{}F is {}°C !", input, result);
break;
} else {
println!("{}°C is {}F !", input, ((input * 9.0 / 5.0) + 32.0));
break;
}
}
}
So, first of all, how can the above code be improved made shorter and how can I avoid so much error handling and DRY out common blocks of code like error handling logic, etc.
Some questions...
- Can I call it a functional language?
- Is there any REPL where I can try out snippets of code, currently I use the online rust playground, it works fine but I am used to something like
irb
in ruby oriex
in elixir. - From my 2 days experience with rust, iI feel like writing code in rust is a lot of work, like I have to specify data types, arrays are fixed-length, etc. Maybe because I have been using some other languages before which felt easier I guess.
- Any project idea I might make in rust(after I learn the language of course) which might give me some experience with writing code in rust and also something which has some utility, like in elixir I made this to learn about genservers in elixir.
- What could be the use cases of running rust code from elixir, I found this.
I hear rust can be used to design compilers, OS, system programs, web servers, etc all this sounds very daunting to me. How can I use rust for something more simple (don't know if that makes sense), like how rust can help in web development or day to day coding?
Lastly, any tips on how to proceed, study resources for a newbie...
Thanks to anyone who takes time to read this answer my questions