Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report