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

简介: Rust vs Go:常用语法对比(四)(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);
}
目录
相关文章
|
2月前
|
Rust API
【Rust学习】09_方法语法
结构体让你可以创建出在你的领域中有意义的自定义类型。通过结构体,我们可以将相关联的数据片段联系起来并命名它们,这样可以使得代码更加清晰。在 impl 块中,你可以定义与你的类型相关联的函数,而方法是一种相关联的函数,允许您指定结构体的实例具有的行为。 但是结构体并不是创建自定义类型的唯一方式:让我们转向 Rust 的 enum 功能,将另一个工具添加到你的工具箱中。
20 0
|
3月前
|
Java 编译器 Go
Go to Learn Go之基础语法
Go to Learn Go之基础语法
23 0
|
4月前
|
Rust 安全 编译器
30天拿下Rust之语法大全
Rust是一种系统级编程语言,以其独特的所有权系统和内存安全性受到开发者青睐。本文从基本数据类型入手,介绍了标量类型如整数、浮点数、布尔值及字符,复合类型如元组、数组和结构体等。此外,还探讨了变量与常量的声明与使用,条件判断与循环语句的语法,以及函数定义与调用的方法。文章通过示例代码展示了如何使用Rust编写简洁高效的程序,并简要介绍了注释与宏的概念,为读者快速掌握这门语言提供了实用指南。欲获取最新文章或交流技术问题,请关注微信公众号“希望睿智”。
58 1
|
3月前
|
Rust Linux Go
Rust/Go语言学习
Rust/Go语言学习
|
6月前
|
存储 Java Go
|
5月前
|
Rust
Rust 中使用 :: 这种语法的几种情况
Rust 中使用 :: 这种语法的几种情况
|
6月前
|
Rust
Rust的if let语法:更简洁的模式匹配
Rust的if let语法:更简洁的模式匹配
|
6月前
|
编译器 Go 开发者
|
6月前
|
Go
go基础语法结束篇 ——函数与方法
go基础语法结束篇 ——函数与方法
|
6月前
|
编译器 Go 数据安全/隐私保护
go语言入门之路——基础语法
go语言入门之路——基础语法