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);
}
目录
相关文章
|
21天前
|
安全 Java Go
Java vs. Go:并发之争
【4月更文挑战第20天】
31 1
|
21天前
|
算法 Java Go
Go vs Java:内存管理与垃圾回收机制对比
对比了Go和Java的内存管理与垃圾回收机制。Java依赖JVM自动管理内存,使用堆栈内存并采用多种垃圾回收算法,如标记-清除和分代收集。Go则提供更多的手动控制,内存分配与释放由分配器和垃圾回收器协同完成,使用三色标记算法并发回收。示例展示了Java中对象自动创建和销毁,而Go中开发者需注意内存泄漏。选择语言应根据项目需求和技术栈来决定。
|
7天前
|
存储 JSON Java
【字节跳动青训营】后端笔记整理-1 | Go语言入门指南:基础语法和常用特性解析(三)
在 Go 语言里,符合语言习惯的做法是使用一个单独的返回值来传递错误信息。
25 0
|
7天前
|
存储 Go C++
【字节跳动青训营】后端笔记整理-1 | Go语言入门指南:基础语法和常用特性解析(二)
Go 语言中的复合数据类型包括数组、切片(slice)、映射(map)和结构体(struct)。
33 0
|
7天前
|
Java 编译器 Go
【字节跳动青训营】后端笔记整理-1 | Go语言入门指南:基础语法和常用特性解析(一)
本文主要梳理自第六届字节跳动青训营(后端组)-Go语言原理与实践第一节(王克纯老师主讲)。
31 1
|
9天前
|
编译器 Go
Go 语言基础语法
Go 语言基础语法
19 1
|
15天前
|
Rust 安全 Java
Rust 和 Go:如何选择最适合你的编程语言
Rust 和 Go 都是优秀的选择 首先,重要的是要说 Rust 和 Go 都是非常优秀的编程语言。它们都是现代的、强大的,被广泛采用,且提供了卓越的性能。
25 1
|
18天前
|
存储 Go C语言
【GO基础】GO基础语法一
【GO基础】GO基础语法一
|
21天前
|
Java Linux Go
一文带你速通Go语言基础语法
本文是关于Go语言的入门介绍,作者因其简洁高效的特性对Go语言情有独钟。文章首先概述了Go语言的优势,包括快速上手、并发编程简单、设计简洁且功能强大,以及丰富的标准库。接着,文章通过示例展示了如何编写和运行Go代码,包括声明包、导入包和输出语句。此外,还介绍了Go的语法基础,如变量类型(数字、字符串、布尔和复数)、变量赋值、类型转换和默认值。文章还涉及条件分支(if和switch)和循环结构(for)。最后,简要提到了Go函数的定义和多返回值特性,以及一些常见的Go命令。作者计划在后续文章中进一步探讨Go语言的其他方面。
19 0
|
21天前
|
Java Go 云计算
【Go语言专栏】Go语言语法基础详解
【4月更文挑战第30天】Go语言,因简洁、高效和并发处理能力深受开发者喜爱,尤其在云计算和微服务领域广泛应用。本文为初学者详解Go语法基础,包括静态类型、垃圾回收、并发编程、错误处理和包管理。通过学习,读者将理解Go语言设计哲学,掌握其语法,以提升开发效率。Go的并发核心是协程和通道,而错误处理采用显式方式,增强了代码的健壮性。Go的包管理机制支持模块化开发。了解这些基础,将助你更好地探索Go语言的世界及其未来潜力。