Fahrenheit and Celsius converter in Rust
up vote
4
down vote
favorite
I made a program that converts between Celsius and Fahrenheit. Is there any way that I could make this code more efficient and cleaner?
use std::io;
// C to F: F = C*(9/5) + 32
// F to C: C = (F-32)*(5/9)
/**********Converts between Fahrenheit and Celsius*********/
fn main() -> () {
println!("Do you want to convert to Celsius or Fahrenheit? Input C or F");
let mut convert_type = String::new();
io::stdin().read_line(&mut convert_type)
.expect("Failed to conversion type.");
let t = String::from(convert_type);
println!("You want to convert to: {}", t);
println!("What temperature would you like to convert?");
let mut temp = String::new();
io::stdin().read_line(&mut temp)
.expect("Failed to read temperature.");
let temp: i32 = match temp.trim().parse() {
Ok(temp) => temp,
Err(_e) => {
-1
}
};
match t.as_str() {
"Cn" => println!("{}", ftoc(temp)),
"Fn" => println!("{}", ctof(temp)),
_ => println!("t = {:?}", t),
}
}
// Celsius to Fahrenheit
fn ctof(c: i32) -> i32 {
(c * (9 / 5)) + 32
}
//Fahrenheit to Celsius
fn ftoc(f: i32) -> i32 {
(f-32) * (5 / 9)
}
Output:
cargo run
Compiling ftoc v0.1.0 (/Users/roberthayek/rustprojects/ftoc)
Finished dev [unoptimized + debuginfo] target(s) in 2.64s
Running `target/debug/ftoc`
Do you want to convert to Celsius or Fahrenheit? Input C or F
F
You want to convert to: F
What temperature would you like to convert?
0
32
rust unit-conversion
New contributor
add a comment |
up vote
4
down vote
favorite
I made a program that converts between Celsius and Fahrenheit. Is there any way that I could make this code more efficient and cleaner?
use std::io;
// C to F: F = C*(9/5) + 32
// F to C: C = (F-32)*(5/9)
/**********Converts between Fahrenheit and Celsius*********/
fn main() -> () {
println!("Do you want to convert to Celsius or Fahrenheit? Input C or F");
let mut convert_type = String::new();
io::stdin().read_line(&mut convert_type)
.expect("Failed to conversion type.");
let t = String::from(convert_type);
println!("You want to convert to: {}", t);
println!("What temperature would you like to convert?");
let mut temp = String::new();
io::stdin().read_line(&mut temp)
.expect("Failed to read temperature.");
let temp: i32 = match temp.trim().parse() {
Ok(temp) => temp,
Err(_e) => {
-1
}
};
match t.as_str() {
"Cn" => println!("{}", ftoc(temp)),
"Fn" => println!("{}", ctof(temp)),
_ => println!("t = {:?}", t),
}
}
// Celsius to Fahrenheit
fn ctof(c: i32) -> i32 {
(c * (9 / 5)) + 32
}
//Fahrenheit to Celsius
fn ftoc(f: i32) -> i32 {
(f-32) * (5 / 9)
}
Output:
cargo run
Compiling ftoc v0.1.0 (/Users/roberthayek/rustprojects/ftoc)
Finished dev [unoptimized + debuginfo] target(s) in 2.64s
Running `target/debug/ftoc`
Do you want to convert to Celsius or Fahrenheit? Input C or F
F
You want to convert to: F
What temperature would you like to convert?
0
32
rust unit-conversion
New contributor
1
Please userustfmt
the next time before pasting your code here so it fulfills the rust style guidelines which helps us reading your code.
– hellow
Nov 12 at 15:53
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I made a program that converts between Celsius and Fahrenheit. Is there any way that I could make this code more efficient and cleaner?
use std::io;
// C to F: F = C*(9/5) + 32
// F to C: C = (F-32)*(5/9)
/**********Converts between Fahrenheit and Celsius*********/
fn main() -> () {
println!("Do you want to convert to Celsius or Fahrenheit? Input C or F");
let mut convert_type = String::new();
io::stdin().read_line(&mut convert_type)
.expect("Failed to conversion type.");
let t = String::from(convert_type);
println!("You want to convert to: {}", t);
println!("What temperature would you like to convert?");
let mut temp = String::new();
io::stdin().read_line(&mut temp)
.expect("Failed to read temperature.");
let temp: i32 = match temp.trim().parse() {
Ok(temp) => temp,
Err(_e) => {
-1
}
};
match t.as_str() {
"Cn" => println!("{}", ftoc(temp)),
"Fn" => println!("{}", ctof(temp)),
_ => println!("t = {:?}", t),
}
}
// Celsius to Fahrenheit
fn ctof(c: i32) -> i32 {
(c * (9 / 5)) + 32
}
//Fahrenheit to Celsius
fn ftoc(f: i32) -> i32 {
(f-32) * (5 / 9)
}
Output:
cargo run
Compiling ftoc v0.1.0 (/Users/roberthayek/rustprojects/ftoc)
Finished dev [unoptimized + debuginfo] target(s) in 2.64s
Running `target/debug/ftoc`
Do you want to convert to Celsius or Fahrenheit? Input C or F
F
You want to convert to: F
What temperature would you like to convert?
0
32
rust unit-conversion
New contributor
I made a program that converts between Celsius and Fahrenheit. Is there any way that I could make this code more efficient and cleaner?
use std::io;
// C to F: F = C*(9/5) + 32
// F to C: C = (F-32)*(5/9)
/**********Converts between Fahrenheit and Celsius*********/
fn main() -> () {
println!("Do you want to convert to Celsius or Fahrenheit? Input C or F");
let mut convert_type = String::new();
io::stdin().read_line(&mut convert_type)
.expect("Failed to conversion type.");
let t = String::from(convert_type);
println!("You want to convert to: {}", t);
println!("What temperature would you like to convert?");
let mut temp = String::new();
io::stdin().read_line(&mut temp)
.expect("Failed to read temperature.");
let temp: i32 = match temp.trim().parse() {
Ok(temp) => temp,
Err(_e) => {
-1
}
};
match t.as_str() {
"Cn" => println!("{}", ftoc(temp)),
"Fn" => println!("{}", ctof(temp)),
_ => println!("t = {:?}", t),
}
}
// Celsius to Fahrenheit
fn ctof(c: i32) -> i32 {
(c * (9 / 5)) + 32
}
//Fahrenheit to Celsius
fn ftoc(f: i32) -> i32 {
(f-32) * (5 / 9)
}
Output:
cargo run
Compiling ftoc v0.1.0 (/Users/roberthayek/rustprojects/ftoc)
Finished dev [unoptimized + debuginfo] target(s) in 2.64s
Running `target/debug/ftoc`
Do you want to convert to Celsius or Fahrenheit? Input C or F
F
You want to convert to: F
What temperature would you like to convert?
0
32
rust unit-conversion
rust unit-conversion
New contributor
New contributor
edited Nov 12 at 16:00
200_success
127k15148410
127k15148410
New contributor
asked Nov 12 at 15:38
roberthayek
212
212
New contributor
New contributor
1
Please userustfmt
the next time before pasting your code here so it fulfills the rust style guidelines which helps us reading your code.
– hellow
Nov 12 at 15:53
add a comment |
1
Please userustfmt
the next time before pasting your code here so it fulfills the rust style guidelines which helps us reading your code.
– hellow
Nov 12 at 15:53
1
1
Please use
rustfmt
the next time before pasting your code here so it fulfills the rust style guidelines which helps us reading your code.– hellow
Nov 12 at 15:53
Please use
rustfmt
the next time before pasting your code here so it fulfills the rust style guidelines which helps us reading your code.– hellow
Nov 12 at 15:53
add a comment |
2 Answers
2
active
oldest
votes
up vote
6
down vote
The first thing I always do is running clippy
.
You will catch some things that are not neccessary, e.g.
fn main() -> ()
can be reduced tofn main()
let t = String::from(convert_type);
is simplylet t = convert_type
The bad things are
c * (9 / 5)
which is alwaysc
because of integer arithmetic. You probably wantf64::from(c) * (9.0 / 5.0)
- same for
(f - 32) * (5 / 9)
should be(f64::from(f) - 32.0) * (5.0 / 9.0)) as i32
You may want to add some unit-tests to your program to verify that ctof
and ftoc
actually work.
New contributor
add a comment |
up vote
2
down vote
Hellow got most of the good parts, but, I'd also recommend figuring out how you might want to signal the user so that an invalid input isn't accepted, since -1 is a temperature someone might want to convert.
In this case I would do something like replacing
let temp: i32 = match temp.trim().parse() {
Ok(temp) => temp,
Err(_e) => {
-1
}
};
with
let temp= match temp.trim().parse() {
Err(_e) =>{
panic!("That wasn't valid input! Temperatures can only be integers!");
}
Ok(i)=>i
};
If I knew this program were to be used solely interactively, then I'd consider adding a loop to give the user another attempt should their input fail.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
The first thing I always do is running clippy
.
You will catch some things that are not neccessary, e.g.
fn main() -> ()
can be reduced tofn main()
let t = String::from(convert_type);
is simplylet t = convert_type
The bad things are
c * (9 / 5)
which is alwaysc
because of integer arithmetic. You probably wantf64::from(c) * (9.0 / 5.0)
- same for
(f - 32) * (5 / 9)
should be(f64::from(f) - 32.0) * (5.0 / 9.0)) as i32
You may want to add some unit-tests to your program to verify that ctof
and ftoc
actually work.
New contributor
add a comment |
up vote
6
down vote
The first thing I always do is running clippy
.
You will catch some things that are not neccessary, e.g.
fn main() -> ()
can be reduced tofn main()
let t = String::from(convert_type);
is simplylet t = convert_type
The bad things are
c * (9 / 5)
which is alwaysc
because of integer arithmetic. You probably wantf64::from(c) * (9.0 / 5.0)
- same for
(f - 32) * (5 / 9)
should be(f64::from(f) - 32.0) * (5.0 / 9.0)) as i32
You may want to add some unit-tests to your program to verify that ctof
and ftoc
actually work.
New contributor
add a comment |
up vote
6
down vote
up vote
6
down vote
The first thing I always do is running clippy
.
You will catch some things that are not neccessary, e.g.
fn main() -> ()
can be reduced tofn main()
let t = String::from(convert_type);
is simplylet t = convert_type
The bad things are
c * (9 / 5)
which is alwaysc
because of integer arithmetic. You probably wantf64::from(c) * (9.0 / 5.0)
- same for
(f - 32) * (5 / 9)
should be(f64::from(f) - 32.0) * (5.0 / 9.0)) as i32
You may want to add some unit-tests to your program to verify that ctof
and ftoc
actually work.
New contributor
The first thing I always do is running clippy
.
You will catch some things that are not neccessary, e.g.
fn main() -> ()
can be reduced tofn main()
let t = String::from(convert_type);
is simplylet t = convert_type
The bad things are
c * (9 / 5)
which is alwaysc
because of integer arithmetic. You probably wantf64::from(c) * (9.0 / 5.0)
- same for
(f - 32) * (5 / 9)
should be(f64::from(f) - 32.0) * (5.0 / 9.0)) as i32
You may want to add some unit-tests to your program to verify that ctof
and ftoc
actually work.
New contributor
edited Nov 12 at 15:53
New contributor
answered Nov 12 at 15:47
hellow
1615
1615
New contributor
New contributor
add a comment |
add a comment |
up vote
2
down vote
Hellow got most of the good parts, but, I'd also recommend figuring out how you might want to signal the user so that an invalid input isn't accepted, since -1 is a temperature someone might want to convert.
In this case I would do something like replacing
let temp: i32 = match temp.trim().parse() {
Ok(temp) => temp,
Err(_e) => {
-1
}
};
with
let temp= match temp.trim().parse() {
Err(_e) =>{
panic!("That wasn't valid input! Temperatures can only be integers!");
}
Ok(i)=>i
};
If I knew this program were to be used solely interactively, then I'd consider adding a loop to give the user another attempt should their input fail.
add a comment |
up vote
2
down vote
Hellow got most of the good parts, but, I'd also recommend figuring out how you might want to signal the user so that an invalid input isn't accepted, since -1 is a temperature someone might want to convert.
In this case I would do something like replacing
let temp: i32 = match temp.trim().parse() {
Ok(temp) => temp,
Err(_e) => {
-1
}
};
with
let temp= match temp.trim().parse() {
Err(_e) =>{
panic!("That wasn't valid input! Temperatures can only be integers!");
}
Ok(i)=>i
};
If I knew this program were to be used solely interactively, then I'd consider adding a loop to give the user another attempt should their input fail.
add a comment |
up vote
2
down vote
up vote
2
down vote
Hellow got most of the good parts, but, I'd also recommend figuring out how you might want to signal the user so that an invalid input isn't accepted, since -1 is a temperature someone might want to convert.
In this case I would do something like replacing
let temp: i32 = match temp.trim().parse() {
Ok(temp) => temp,
Err(_e) => {
-1
}
};
with
let temp= match temp.trim().parse() {
Err(_e) =>{
panic!("That wasn't valid input! Temperatures can only be integers!");
}
Ok(i)=>i
};
If I knew this program were to be used solely interactively, then I'd consider adding a loop to give the user another attempt should their input fail.
Hellow got most of the good parts, but, I'd also recommend figuring out how you might want to signal the user so that an invalid input isn't accepted, since -1 is a temperature someone might want to convert.
In this case I would do something like replacing
let temp: i32 = match temp.trim().parse() {
Ok(temp) => temp,
Err(_e) => {
-1
}
};
with
let temp= match temp.trim().parse() {
Err(_e) =>{
panic!("That wasn't valid input! Temperatures can only be integers!");
}
Ok(i)=>i
};
If I knew this program were to be used solely interactively, then I'd consider adding a loop to give the user another attempt should their input fail.
answered Nov 12 at 21:19
jaked122
13116
13116
add a comment |
add a comment |
roberthayek is a new contributor. Be nice, and check out our Code of Conduct.
roberthayek is a new contributor. Be nice, and check out our Code of Conduct.
roberthayek is a new contributor. Be nice, and check out our Code of Conduct.
roberthayek is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207481%2ffahrenheit-and-celsius-converter-in-rust%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please use
rustfmt
the next time before pasting your code here so it fulfills the rust style guidelines which helps us reading your code.– hellow
Nov 12 at 15:53