2019年7月12日金曜日

開発環境

プログラミング言語Rust 公式ガイド (Steve Klabnik(著)、Carol Nichols(著)、尾崎 亮太(翻訳)、KADOKAWA)の第3章(一般的なプログラミングの概念)の摂氏を華氏に、華氏を摂氏に変換するプログラムのソースコードのコメント中以外にも日本語を含めて書いてみる。(コメント中以外に日本語文字があるとコンパイルに失敗することがあるとの記述があったから、失敗する場合をちょっとずつ探してみる。)

コード

// 変数名、関数名はascii文字しかサポートされてないみたい
// fn 摂氏を華氏に変換(摂氏: f64) -> f64 {
//     摂氏 * 9 / 5 + 32
// }
// fn 華氏を摂氏に変換(華氏: f64) -> f64 {
//     (華氏 - 32) * 5 / 9
// }
fn celsius_to_fahrenheit(c: f64) -> f64 {
    c * 9. / 5. + 32.
}
fn fahrenheit_to_celsius(f: f64) -> f64 {
    (f - 32.) * 5. / 9.
}
fn main() {
    let cs = [1., 2., 3., 4., 5.];
    for c in cs.iter() {
        // let 華氏 = 摂氏を華氏に変換(c);
        // println!("{}℃ = {}℉", c, 華氏);
        let f: f64 = celsius_to_fahrenheit(*c);
        println!("摂氏を華氏に変換: {}℃ = {}℉", *c, f);
        let c = fahrenheit_to_celsius(f);
        println!("華氏を摂氏に変換: {}℃ = {}℉", c, f);
    }
}

入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)

$ cargo check
    Checking sample1 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample1)
error[E0308]: mismatched types
  --> src/main.rs:19:44
   |
19 |         let f: f64 = celsius_to_fahrenheit(c);
   |                                            ^
   |                                            |
   |                                            expected f64, found &{float}
   |                                            help: consider dereferencing the borrow: `*c`
   |
   = note: expected type `f64`
              found type `&{float}`

error[E0277]: can't compare `&{float}` with `f64`
  --> src/main.rs:24:15
   |
24 |             c == fahrenheit_to_celsius(f)
   |               ^^ no implementation for `&{float} == f64`
   |
   = help: the trait `std::cmp::PartialEq<f64>` is not implemented for `&{float}`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
error: Could not compile `sample1`.

To learn more, run the command again with --verbose.
$ cargo check
    Checking sample1 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample1)
    Finished dev [unoptimized + debuginfo] target(s) in 0.39s
$ cargo run
   Compiling sample1 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample1)
    Finished dev [unoptimized + debuginfo] target(s) in 0.50s
     Running `target/debug/sample1`
1℃ = 33.8℉ (逆演算を比較 false)
2℃ = 35.6℉ (逆演算を比較 false)
3℃ = 37.4℉ (逆演算を比較 false)
4℃ = 39.2℉ (逆演算を比較 false)
5℃ = 41℉ (逆演算を比較 true)
$ cargo run
   Compiling sample1 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample1)
    Finished dev [unoptimized + debuginfo] target(s) in 0.51s
     Running `target/debug/sample1`
1℃ = 33.8℉
0.9999999999999984℃ = 33.8℉
2℃ = 35.6℉
2.000000000000001℃ = 35.6℉
3℃ = 37.4℉
2.999999999999999℃ = 37.4℉
4℃ = 39.2℉
4.000000000000002℃ = 39.2℉
5℃ = 41℉
5℃ = 41℉
$ cargo run
   Compiling sample1 v0.1.0 (/.../rust/プログラミング言語Rust 公式ガイド/ch3/sample1)
    Finished dev [unoptimized + debuginfo] target(s) in 0.25s
     Running `target/debug/sample1`
摂氏を華氏に変換: 1℃ = 33.8℉
華氏を摂氏に変換: 0.9999999999999984℃ = 33.8℉
摂氏を華氏に変換: 2℃ = 35.6℉
華氏を摂氏に変換: 2.000000000000001℃ = 35.6℉
摂氏を華氏に変換: 3℃ = 37.4℉
華氏を摂氏に変換: 2.999999999999999℃ = 37.4℉
摂氏を華氏に変換: 4℃ = 39.2℉
華氏を摂氏に変換: 4.000000000000002℃ = 39.2℉
摂氏を華氏に変換: 5℃ = 41℉
華氏を摂氏に変換: 5℃ = 41℉
$ 

変数名、関数名に日本語(ASCII文字)以外は使えないみたい。

1 コメント :

匿名さんのコメント...

nightlyで `feature(non_ascii_idents)` をつければ使えます

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=8675a573985fbce823cf2ea4abb75e20

コメントを投稿