Rust vs Go:常用语法对比(4)(2)

简介: Rust vs Go:常用语法对比(4)(2)

71. Echo program implementation

Basic implementation of the Echo program: Print all arguments except the program name, separated by space, followed by newline.

The idiom demonstrates how to skip the first argument if necessary, concatenate arguments as strings, append newline and print it to stdout.

实现 Echo 程序


package main
import "fmt"
import "os"
import "strings"
func main() {
    fmt.Println(strings.Join(os.Args[1:], " "))
}
use std::env;
fn main() {
    println!("{}", env::args().skip(1).collect::<Vec<_>>().join(" "));
}

or

use itertools::Itertools;
println!("{}", std::env::args().skip(1).format(" "));

74. Compute GCD

Compute the greatest common divisor x of big integers a and b. Use an integer type able to handle huge numbers.

计算大整数a和b的最大公约数x。使用能够处理大数的整数类型。

package main
import "fmt"
import "math/big"
func main() {
  a, b, x := new(big.Int), new(big.Int), new(big.Int)
  a.SetString("6000000000000", 10)
  b.SetString("9000000000000", 10)
  x.GCD(nil, nil, a, b)
  fmt.Println(x)
}
extern crate num;
use num::Integer;
use num::bigint::BigInt;
fn main() {
    let a = BigInt::parse_bytes(b"6000000000000", 10).unwrap();
    let b = BigInt::parse_bytes(b"9000000000000", 10).unwrap();
    let x = a.gcd(&b);
    println!("{}", x);
}

75. Compute LCM

计算大整数a和b的最小公倍数x。使用能够处理大数的整数类型。

Compute the least common multiple x of big integers a and b. Use an integer type able to handle huge numbers.


package main
import "fmt"
import "math/big"
func main() {
  a, b, gcd, x := new(big.Int), new(big.Int), new(big.Int), new(big.Int)
  a.SetString("6000000000000", 10)
  b.SetString("9000000000000", 10)
  gcd.GCD(nil, nil, a, b)
  x.Div(a, gcd).Mul(x, b)
  fmt.Println(x)
}
extern crate num;
use num::bigint::BigInt;
use num::Integer;
fn main() {
    let a = BigInt::parse_bytes(b"6000000000000", 10).unwrap();
    let b = BigInt::parse_bytes(b"9000000000000", 10).unwrap();
    let x = a.lcm(&b);
    println!("x = {}", x);
}

76. Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

将十进制整数转换为二进制数字

package main
import "fmt"
import "strconv"
func main() {
  x := int64(13)
  s := strconv.FormatInt(x, 2)
  fmt.Println(s)
}

or

package main
import (
  "fmt"
  "math/big"
)
func main() {
  x := big.NewInt(13)
  s := fmt.Sprintf("%b", x)
  fmt.Println(s)
}
fn main() {
    let x = 13;
    let s = format!("{:b}", x);
    println!("{}", s);
}

77. SComplex number

Declare a complex x and initialize it with value (3i - 2). Then multiply it by i.

复数

package main
import (
  "fmt"
  "reflect"
)
func main() {
  x := 3i - 2
  x *= 1i
  fmt.Println(x)
  fmt.Print(reflect.TypeOf(x))
}
(-3-2i)
complex128
extern crate num;
use num::Complex;
fn main() {
    let mut x = Complex::new(-2, 3);
    x *= Complex::i();
    println!("{}", x);
}

78. "do while" loop

Execute a block once, then execute it again as long as boolean condition c is true.

循环执行

package main
import (
  "fmt"
  "math/rand"
)
func main() {
  for {
    x := rollDice()
    fmt.Println("Got", x)
    if x == 3 {
      break
    }
  }
}
func rollDice() int {
  return 1 + rand.Intn(6)
}

Go has no do while loop, use the for loop, instead.

Got 6
Got 4
Got 6
Got 6
Got 2
Got 1
Got 2
Got 3

or

package main
import (
  "fmt"
  "math/rand"
)
func main() {
  for done := false; !done; {
    x := rollDice()
    fmt.Println("Got", x)
    done = x == 3
  }
}
func rollDice() int {
  return 1 + rand.Intn(6)
}
Got 6
Got 4
Got 6
Got 6
Got 2
Got 1
Got 2
Got 3
loop {
    doStuff();
    if !c { break; }
}

Rust has no do-while loop with syntax sugar. Use loop and break.


79. Convert integer to floating point number

Declare floating point number y and initialize it with the value of integer x .

整型转浮点型

声明浮点数y并用整数x的值初始化它。

package main
import (
  "fmt"
  "reflect"
)
func main() {
  x := 5
  y := float64(x)
  fmt.Println(y)
  fmt.Printf("%.2f\n", y)
  fmt.Println(reflect.TypeOf(y))
}
5
5.00
float64
fn main() {
    let i = 5;
    let f = i as f64;
    println!("int {:?}, float {:?}", i, f);
}
int 5, float 5.0

80.  Truncate floating point number to integer

Declare integer y and initialize it with the value of floating point number x . Ignore non-integer digits of x . Make sure to truncate towards zero: a negative x must yield the closest greater integer (not lesser).

浮点型转整型

package main
import "fmt"
func main() {
  a := -6.4
  b := 6.4
  c := 6.6
  fmt.Println(int(a))
  fmt.Println(int(b))
  fmt.Println(int(c))
}
-6
6
6
fn main() {
    let x = 41.59999999f64;
    let y = x as i32;
    println!("{}", y);
}
目录
相关文章
|
1月前
|
Rust 安全 程序员
|
25天前
|
Rust 安全 Java
Rust 和 Go:如何选择最适合你的编程语言
Rust 和 Go 都是优秀的选择 首先,重要的是要说 Rust 和 Go 都是非常优秀的编程语言。它们都是现代的、强大的,被广泛采用,且提供了卓越的性能。
25 1
|
1月前
|
Rust 安全 程序员
Rust vs Go:解析两者的独特特性和适用场景
在讨论 Rust 与 Go 两种编程语言哪种更优秀时,我们将探讨它们在性能、简易性、安全性、功能、规模和并发处理等方面的比较。同时,我们看看它们有什么共同点和根本的差异。现在就来看看这个友好而公平的对比。
|
10月前
|
Rust Go C++
Rust vs Go:常用语法对比(十三)(1)
Rust vs Go:常用语法对比(十三)(1)
65 0
|
10月前
|
Rust Go C++
Rust vs Go:常用语法对比(十三)(2)
Rust vs Go:常用语法对比(十三)(2)
78 1
|
10月前
|
Rust Go C++
Rust vs Go:常用语法对比(十二)(2)
Rust vs Go:常用语法对比(十二)(2)
64 0
|
10月前
|
Rust Go C++
Rust vs Go:常用语法对比(十二)(1)
Rust vs Go:常用语法对比(十二)(1)
67 0
|
10月前
|
Rust Go C++
Rust vs Go:常用语法对比(十一)(2)
Rust vs Go:常用语法对比(十一)(2)
56 0
|
10月前
|
Rust Go C++
Rust vs Go:常用语法对比(十一)(1)
Rust vs Go:常用语法对比(十一)(1)
49 0
|
10月前
|
Rust Go C++
Rust vs Go:常用语法对比(十)(2)
Rust vs Go:常用语法对比(十)(2)
84 0