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);
}
目录
相关文章
|
算法 Go
Go 语言泛型 — 泛型语法与示例
本文详解 Go 语言泛型语法与使用示例,涵盖泛型函数、类型声明、类型约束及实战应用,适合入门学习与开发实践。
|
10月前
|
存储 安全 Java
【Golang】(4)Go里面的指针如何?函数与方法怎么不一样?带你了解Go不同于其他高级语言的语法
结构体可以存储一组不同类型的数据,是一种符合类型。Go抛弃了类与继承,同时也抛弃了构造方法,刻意弱化了面向对象的功能,Go并非是一个传统OOP的语言,但是Go依旧有着OOP的影子,通过结构体和方法也可以模拟出一个类。
450 2
|
人工智能 IDE 程序员
Go 官方宣布不再改进错误处理语法,背后原因是什么?
Go 官方发布了一篇博客文章,正式宣布:他们不会再推进任何新的错误处理语法提案。这也意味着,未来编写 Go 代码时,你依然会频繁地写下那句熟悉的 if err != nil {return err}。
221 0
|
Rust 安全 编译器
30天拿下Rust之语法大全
Rust是一种系统级编程语言,以其独特的所有权系统和内存安全性受到开发者青睐。本文从基本数据类型入手,介绍了标量类型如整数、浮点数、布尔值及字符,复合类型如元组、数组和结构体等。此外,还探讨了变量与常量的声明与使用,条件判断与循环语句的语法,以及函数定义与调用的方法。文章通过示例代码展示了如何使用Rust编写简洁高效的程序,并简要介绍了注释与宏的概念,为读者快速掌握这门语言提供了实用指南。欲获取最新文章或交流技术问题,请关注微信公众号“希望睿智”。
507 1
|
Rust API
【Rust学习】09_方法语法
结构体让你可以创建出在你的领域中有意义的自定义类型。通过结构体,我们可以将相关联的数据片段联系起来并命名它们,这样可以使得代码更加清晰。在 impl 块中,你可以定义与你的类型相关联的函数,而方法是一种相关联的函数,允许您指定结构体的实例具有的行为。 但是结构体并不是创建自定义类型的唯一方式:让我们转向 Rust 的 enum 功能,将另一个工具添加到你的工具箱中。
236 0
|
Java 编译器 Go
Go to Learn Go之基础语法
Go to Learn Go之基础语法
227 0
|
Rust Linux Go
Rust/Go语言学习
Rust/Go语言学习
Rust 中使用 :: 这种语法的几种情况
Rust 中使用 :: 这种语法的几种情况
Rust的if let语法:更简洁的模式匹配
Rust的if let语法:更简洁的模式匹配
360 0
|
编译器 Go 开发者
Go语言语法基础入门
Go语言语法基础入门