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