Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common features / libraries of Golang and Rust languages

Shulou Source: shulou.com Published: 2022-06-02 23:56:35 09月22日 Update

This article introduces the knowledge of "what are the common functions / libraries of Golang and Rust language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Parameter processing

Features are provided in the Golang standard library. The flag library is a very basic function, and it is also very useful in practice. It is essential when doing command-line interactive programs.

Package main import "flag" var (program = flag.String ("p", "", "h program to compile/run") outFname = flag.String ("o", "", "if specified, write the webassembly binary created by-p here") watFname = flag.String ("o-wat", "", "if specified, write the uncompiled webassembly created by-p here") port = flag.String ("port", "", "HTTP port to listen on") writeTao = flag.Bool ("koan", false) "if true, print the h koan and then exit") writeVersion = flag.Bool ("v", false, "if true, print the version of h and then exit")

The above code generates some package global variables that will contain the values of the command line arguments.

In Rust, the common command-line parsing package is structopt. However, it works differently from the Golang flag package. Structopt loads options into the structure rather than global mutable variables. The main reason is that globally variable variables are basically avoided in Rust programming practice. In most cases, the global variable flag with packages is fine, but only if it is written to the program before it actually starts doing what it needs to do.

A simple example comes from the pahi library source code:

# [derive (Debug, StructOpt)] # [structopt (name = "pa'i", about = "A WebAssembly runtime in Rust meeting the Olin ABI.")] struct Opt {# [structopt (short, long, default_value = "cranelift")] backend: String, # [structopt (short, long)] function_log: bool, # [structopt (short, long)] no_cache: bool, # [structopt ()] fname: String, # [structopt (short, long, default_value = "_ start")] entrypoint: String # [structopt ()] args: Vec,}

The Rust compiler generates the required parameter parsing code, and then you can use:

Fn main () {let opt = Opt::from_args (); debug! ("args: {:}", opt.args); if opt.backend! = "cranelift" {return Err (format! ("wanted backend to be cranelift, got: {}", opt.backend));}

Error handling

Golang's standard library has an error interface, which can create a function that describes the type, and the function describes why the function cannot be performed as expected. Golang programs must first handle errors. For example:

Func Write (w io.Writer, buf [] byte) error {_, err: = w.Write (buf) if err! = nil {log.Println ("unable to write:", err) return err} return nil

Rust also has the Error feature, which also allows you to create a type that describes why the function fails to perform its intended function. Here we introduce the easier-to-use thiserror crate build custom error types:

[dependencies] thiserror = "1"

Then, use in the program:

Use std::fmt; use thiserror::Error; # [derive (Debug, Error)] struct Divide0; impl fmt::Display for Divide0 {fn fmt (& self, f: & mut fmt::Formatter)

Tags: Program function log type parameter standard run code server template error processing work service global content function variable command customer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux Shulou Information Microsoft Xiaomi