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

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

191. Check if any value in a list is larger than a limit

Given a one-dimensional array a, check if any value is larger than x, and execute the procedure f if that is the case

检查列表中是否有任何值大于限制

package main
import (
  "fmt"
)
func f() {
  fmt.Println("Larger found")
}
func main() {
  a := []int{1, 2, 3, 4, 5}
  x := 4
  for _, v := range a {
    if v > x {
      f()
      break
    }
  }
}
fn main() {
    let a = [5, 6, 8, -20, 9, 42];
    let x = 35;
    if a.iter().any(|&elem| elem > x) {
        f()
    }
    let x = 50;
    if a.iter().any(|&elem| elem > x) {
        g()
    }
}
fn f() {
    println!("F")
}
fn g() {
    println!("G")
}

192. Declare a real variable with at least 20 digits

Declare a real variable a with at least 20 digits; if the type does not exist, issue an error at compile time.

声明一个至少有20位数字的实变量

package main
import (
  "fmt"
  "math/big"
)
func main() {
  a, _, err := big.ParseFloat("123456789.123456789123465789", 10, 200, big.ToZero)
  if err != nil {
    panic(err)
  }
  fmt.Println(a)
}
use rust_decimal::Decimal;
use std::str::FromStr;
let a = Decimal::from_str("1234567890.123456789012345").unwrap();

197.  Get a list of lines from a file

Retrieve the contents of file at path into a list of strings lines, in which each element is a line of the file.

从文件中获取行列表.将文件路径中的内容检索到字符串行列表中,其中每个元素都是文件的一行。

package main
import (
  "fmt"
  "io/ioutil"
  "log"
  "strings"
)
func readLines(path string) ([]string, error) {
  b, err := ioutil.ReadFile(path)
  if err != nil {
    return nil, err
  }
  lines := strings.Split(string(b), "\n")
  return lines, nil
}
func main() {
  lines, err := readLines("/tmp/file1")
  if err != nil {
    log.Fatalln(err)
  }
  for i, line := range lines {
    fmt.Printf("line %d: %s\n", i, line)
  }
}
func init() {
  data := []byte(`foo
bar
baz`)
  err := ioutil.WriteFile("/tmp/file1", data, 0644)
  if err != nil {
    log.Fatalln(err)
  }
}
line 0: foo
line 1: bar
line 2: baz
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
fn main() {
    let path = "/etc/hosts";
    let lines = BufReader::new(File::open(path).unwrap())
        .lines()
        .collect::<Vec<_>>();
    println!("{:?}", lines);
}

198. Abort program execution with error condition

Abort program execution with error condition x (where x is an integer value)

出现错误情况时中止程序执行

package main
import (
  "os"
)
func main() {
  x := 1
  os.Exit(x)
}

Program exited: status 1.

use std::process;
process::exit(x);

200. Return hypotenuse

Returns the hypotenuse h of the triangle where the sides adjacent to the square angle have lengths x and y.

返回三角形的斜边h,其中与直角相邻的边的长度为x和y。

package main
import (
  "fmt"
  "math"
)
func main() {
  x := 1.0
  y := 1.0
  h := math.Hypot(x, y)
  fmt.Println(h)
}
fn main() {
    let (x, y) = (1.0, 1.0);
    let h = hypot(x, y);
    println!("{}", h);
}
fn hypot(x: f64, y: f64) -> f64 {
    let num = x.powi(2) + y.powi(2);
    num.powf(0.5)
}
目录
相关文章
|
11天前
|
存储 Java Go
|
3天前
|
Go
go基础语法结束篇 ——函数与方法
go基础语法结束篇 ——函数与方法
|
3天前
|
编译器 Go 数据安全/隐私保护
go语言入门之路——基础语法
go语言入门之路——基础语法
|
24天前
|
Java 编译器 Go
【字节跳动青训营】后端笔记整理-1 | Go语言入门指南:基础语法和常用特性解析(一)
本文主要梳理自第六届字节跳动青训营(后端组)-Go语言原理与实践第一节(王克纯老师主讲)。
45 1
|
26天前
|
编译器 Go
Go 语言基础语法
Go 语言基础语法
23 1
|
4天前
|
存储 安全 Java
【Go语言精进之路】Go语言基础:基础语法概览
【Go语言精进之路】Go语言基础:基础语法概览
15 0
|
11天前
|
Java Go Scala
|
11天前
|
存储 Java Unix
|
24天前
|
存储 JSON Java
【字节跳动青训营】后端笔记整理-1 | Go语言入门指南:基础语法和常用特性解析(三)
在 Go 语言里,符合语言习惯的做法是使用一个单独的返回值来传递错误信息。
31 0
|
24天前
|
存储 Go C++
【字节跳动青训营】后端笔记整理-1 | Go语言入门指南:基础语法和常用特性解析(二)
Go 语言中的复合数据类型包括数组、切片(slice)、映射(map)和结构体(struct)。
45 0