国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Rust vs Go:常用語法對比(五)

這篇具有很好參考價值的文章主要介紹了Rust vs Go:常用語法對比(五)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Rust vs Go:常用語法對比(五),后端

題圖來自 Rust vs Go 2023[1]


81. Round floating point number to integer

Declare integer y and initialize it with the rounded value of floating point number x . Ties (when the fractional part of x is exactly .5) must be rounded up (to positive infinity).

按規(guī)則取整

package?main

import?(
?"fmt"
?"math"
)

func?round(x?float64)?int?{
?y?:=?int(math.Floor(x?+?0.5))
?return?y
}

func?main()?{
?for?_,?x?:=?range?[]float64{-1.1,?-0.9,?-0.5,?-0.1,?0.,?0.1,?0.5,?0.9,?1.1}?{
??fmt.Printf("%5.1f?%5d\n",?x,?round(x))
?}
}
?-1.1????-1
?-0.9????-1
?-0.5?????0
?-0.1?????0
??0.0?????0
??0.1?????0
??0.5?????1
??0.9?????1
??1.1?????1

fn?main()?{
????let?x?:?f64?=?2.71828;
????let?y?=?x.round()?as?i64;
????
????println!("{}?{}",?x,?y);
}

2.71828 3


82. Count substring occurrences

統(tǒng)計子字符串出現(xiàn)次數(shù)

package?main

import?(
?"fmt"
?"strings"
)

func?main()?{
?s?:=?"Romaromamam"
?t?:=?"mam"

?x?:=?strings.Count(s,?t)

?fmt.Println(x)
}

1


fn?main()?{
????let?s?=?"lorem?ipsum?lorem?ipsum?lorem?ipsum?lorem?ipsum";
????let?t?=?"ipsum";

????let?c?=?s.matches(t).count();

????println!("{}?occurrences",?c);
}

Disjoint matches: overlapping occurrences are not counted.

4 occurrences


83. Regex with character repetition

Declare regular expression r matching strings "http", "htttp", "httttp", etc.

正則表達式匹配重復字符

package?main

import?(
?"fmt"
?"regexp"
)

func?main()?{
?r?:=?regexp.MustCompile("htt+p")

?for?_,?s?:=?range?[]string{
??"hp",
??"htp",
??"http",
??"htttp",
??"httttp",
??"htttttp",
??"htttttp",
??"word?htttp?in?a?sentence",
?}?{
??fmt.Println(s,?"=>",?r.MatchString(s))
?}
}
hp?=>?false
htp?=>?false
http?=>?true
htttp?=>?true
httttp?=>?true
htttttp?=>?true
htttttp?=>?true
word?htttp?in?a?sentence?=>?true

extern?crate?regex;
use?regex::Regex;

fn?main()?{
????let?r?=?Regex::new(r"htt+p").unwrap();
????
????assert!(r.is_match("http"));
????assert!(r.is_match("htttp"));
????assert!(r.is_match("httttp"));
}

84. Count bits set in integer binary representation

Count number c of 1s in the integer i in base 2.

E.g. i=6 → c=2

計算十進制整型的二進制表示中 1的個數(shù)

package?main

import?"fmt"

func?PopCountUInt64(i?uint64)?(c?int)?{
?//?bit?population?count,?see
?//?http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
?i?-=?(i?>>?1)?&?0x5555555555555555
?i?=?(i>>2)&0x3333333333333333?+?i&0x3333333333333333
?i?+=?i?>>?4
?i?&=?0x0f0f0f0f0f0f0f0f
?i?*=?0x0101010101010101
?return?int(i?>>?56)
}

func?PopCountUInt32(i?uint32)?(n?int)?{
?//?bit?population?count,?see
?//?http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
?i?-=?(i?>>?1)?&?0x55555555
?i?=?(i>>2)&0x33333333?+?i&0x33333333
?i?+=?i?>>?4
?i?&=?0x0f0f0f0f
?i?*=?0x01010101
?return?int(i?>>?24)
}

func?main()?{
?for?i?:=?uint64(0);?i?<?16;?i++?{
??c?:=?PopCountUInt64(i)
??fmt.Printf("%4d?%04[1]b?%d\n",?i,?c)
?}

?for?i?:=?uint32(0);?i?<?16;?i++?{
??c?:=?PopCountUInt32(i)
??fmt.Printf("%4d?%04[1]b?%d\n",?i,?c)
?}
}
???0?0000?0
???1?0001?1
???2?0010?1
???3?0011?2
???4?0100?1
???5?0101?2
???6?0110?2
???7?0111?3
???8?1000?1
???9?1001?2
??10?1010?2
??11?1011?3
??12?1100?2
??13?1101?3
??14?1110?3
??15?1111?4
???0?0000?0
???1?0001?1
???2?0010?1
???3?0011?2
???4?0100?1
???5?0101?2
???6?0110?2
???7?0111?3
???8?1000?1
???9?1001?2
??10?1010?2
??11?1011?3
??12?1100?2
??13?1101?3
??14?1110?3
??15?1111?4

This was useful only before go 1.9. See math/bits.OnesCount instead

or

package?main

import?(
?"fmt"
?"math/bits"
)

func?main()?{
?for?i?:=?uint(0);?i?<?16;?i++?{
??c?:=?bits.OnesCount(i)
??fmt.Printf("%4d?%04[1]b?%d\n",?i,?c)
?}
}

???0?0000?0
???1?0001?1
???2?0010?1
???3?0011?2
???4?0100?1
???5?0101?2
???6?0110?2
???7?0111?3
???8?1000?1
???9?1001?2
??10?1010?2
??11?1011?3
??12?1100?2
??13?1101?3
??14?1110?3
??15?1111?4

fn?main()?{
????println!("{}",?6usize.count_ones())
}

2


85. Check if integer addition will overflow

檢查兩個整型相加是否溢出

package?main

import?(
?"fmt"
?"math"
)

func?willAddOverflow(a,?b?int64)?bool?{
?return?a?>?math.MaxInt64-b
}

func?main()?{

?fmt.Println(willAddOverflow(11111111111111111,?2))

}

false


fn?adding_will_overflow(x:?usize,?y:?usize)?->?bool?{
????x.checked_add(y).is_none()
}

fn?main()?{
????{
????????let?(x,?y)?=?(2345678,?9012345);

????????let?overflow?=?adding_will_overflow(x,?y);

????????println!(
????????????"{}?+?{}?{}",
????????????x,
????????????y,
????????????if?overflow?{
????????????????"overflows"
????????????}?else?{
????????????????"doesn't?overflow"
????????????}
????????);
????}
????{
????????let?(x,?y)?=?(2345678901,?9012345678);

????????let?overflow?=?adding_will_overflow(x,?y);

????????println!(
????????????"{}?+?{}?{}",
????????????x,
????????????y,
????????????if?overflow?{
????????????????"overflows"
????????????}?else?{
????????????????"doesn't?overflow"
????????????}
????????);
????}
????{
????????let?(x,?y)?=?(2345678901234,?9012345678901);

????????let?overflow?=?adding_will_overflow(x,?y);

????????println!(
????????????"{}?+?{}?{}",
????????????x,
????????????y,
????????????if?overflow?{
????????????????"overflows"
????????????}?else?{
????????????????"doesn't?overflow"
????????????}
????????);
????}
????{
????????let?(x,?y)?=?(23456789012345678,?90123456789012345);

????????let?overflow?=?adding_will_overflow(x,?y);

????????println!(
????????????"{}?+?{}?{}",
????????????x,
????????????y,
????????????if?overflow?{
????????????????"overflows"
????????????}?else?{
????????????????"doesn't?overflow"
????????????}
????????);
????}
????{
????????let?(x,?y)?=?(12345678901234567890,?9012345678901234567);

????????let?overflow?=?adding_will_overflow(x,?y);

????????println!(
????????????"{}?+?{}?{}",
????????????x,
????????????y,
????????????if?overflow?{
????????????????"overflows"
????????????}?else?{
????????????????"doesn't?overflow"
????????????}
????????);
????}
}

2345678?+?9012345?doesn't?overflow
2345678901?+?9012345678?doesn't?overflow
2345678901234?+?9012345678901?doesn't?overflow
23456789012345678?+?90123456789012345?doesn't?overflow
12345678901234567890?+?9012345678901234567?overflows

86. Check if integer multiplication will overflow

檢查整型相乘是否溢出

package?main

import?(
?"fmt"
)

func?multiplyWillOverflow(x,?y?uint64)?bool?{
?if?x?<=?1?||?y?<=?1?{
??return?false
?}
?d?:=?x?*?y
?return?d/y?!=?x
}

func?main()?{
?{
??var?x,?y?uint64?=?2345,?6789
??if?multiplyWillOverflow(x,?y)?{
???fmt.Println(x,?"*",?y,?"overflows")
??}?else?{
???fmt.Println(x,?"*",?y,?"doesn't?overflow")
??}
?}
?{
??var?x,?y?uint64?=?2345678,?9012345
??if?multiplyWillOverflow(x,?y)?{
???fmt.Println(x,?"*",?y,?"overflows")
??}?else?{
???fmt.Println(x,?"*",?y,?"doesn't?overflow")
??}
?}
?{
??var?x,?y?uint64?=?2345678901,?9012345678
??if?multiplyWillOverflow(x,?y)?{
???fmt.Println(x,?"*",?y,?"overflows")
??}?else?{
???fmt.Println(x,?"*",?y,?"doesn't?overflow")
??}
?}
}

2345?*?6789?doesn't?overflow
2345678?*?9012345?doesn'
t?overflow
2345678901?*?9012345678?overflows

fn?main()?{
????{
????????let?(x,?y)?=?(2345,?6789);

????????let?overflow?=?multiply_will_overflow(x,?y);

????????println!(
????????????"{}?*?{}?{}",
????????????x,
????????????y,
????????????if?overflow?{
????????????????"overflows"
????????????}?else?{
????????????????"doesn't?overflow"
????????????}
????????);
????}
????{
????????let?(x,?y)?=?(2345678,?9012345);

????????let?overflow?=?multiply_will_overflow(x,?y);

????????println!(
????????????"{}?*?{}?{}",
????????????x,
????????????y,
????????????if?overflow?{
????????????????"overflows"
????????????}?else?{
????????????????"doesn't?overflow"
????????????}
????????);
????}
????{
????????let?(x,?y)?=?(2345678901,?9012345678);

????????let?overflow?=?multiply_will_overflow(x,?y);

????????println!(
????????????"{}?*?{}?{}",
????????????x,
????????????y,
????????????if?overflow?{
????????????????"overflows"
????????????}?else?{
????????????????"doesn't?overflow"
????????????}
????????);
????}
}

fn?multiply_will_overflow(x:?i64,?y:?i64)?->?bool?{
????x.checked_mul(y).is_none()
}
2345?*?6789?doesn't?overflow
2345678?*?9012345?doesn't?overflow
2345678901?*?9012345678?overflows

87. Stop program

Exit immediately.
If some extra cleanup work is executed by the program runtime (not by the OS itself), describe it.

停止程序,立即退出。

package?main

import?"os"

func?main()?{

?os.Exit(1)

?print(2222)
}


fn?main()?{
????std::process::exit(1);
????println!("42");
}

88. Allocate 1M bytes

分配1M內(nèi)存

package?main

import?"fmt"

func?main()?{
?buf?:=?make([]byte,?1000000)

?for?i,?b?:=?range?buf?{
??if?b?!=?0?{
???fmt.Println("Found?unexpected?value",?b,?"at?position",?i)
??}
?}
?fmt.Println("Buffer?was?correctly?initialized?with?zero?values.")
}

Buffer was correctly initialized with zero values.


fn?main()?{
????let?buf:?Vec<u8>?=?Vec::with_capacity(1024?*?1024);
????println!("{:?}",?buf.capacity());
}

1048576


89. Handle invalid argument

處理無效參數(shù)

package?main

import?"fmt"

//?NewSquareMatrix?creates?a?N-by-N?matrix
func?NewSquareMatrix(N?int)?([][]float64,?error)?{
?if?N?<?0?{
??return?nil,?fmt.Errorf("Invalid?size?%d:?order?cannot?be?negative",?N)
?}
?matrix?:=?make([][]float64,?N)
?for?i?:=?range?matrix?{
??matrix[i]?=?make([]float64,?N)
?}
?return?matrix,?nil
}

func?main()?{
?N1?:=?3
?matrix1,?err1?:=?NewSquareMatrix(N1)
?if?err1?==?nil?{
??fmt.Println(matrix1)
?}?else?{
??fmt.Println(err1)
?}

?N2?:=?-2
?matrix2,?err2?:=?NewSquareMatrix(N2)
?if?err2?==?nil?{
??fmt.Println(matrix2)
?}?else?{
??fmt.Println(err2)
?}
}

[[0?0?0]?[0?0?0]?[0?0?0]]
Invalid?size?-2:?order?cannot?be?negative

#[derive(Debug,?PartialEq,?Eq)]
enum?CustomError?{?InvalidAnswer?}

fn?do_stuff(x:?i32)?->?Result<i32,?CustomError>?{
????if?x?!=?42?{
%2

90. Read-only outside

外部只讀

type?Foo?struct?{
?x?int
}

func?(f?*Foo)?X()?int?{
?return?f.x
}
x?is?private,?because?it?is?not?capitalized.
(*Foo).X?is?a?public?getter?(a?read?accessor).

struct?Foo?{
????x:?usize
}

impl?Foo?{
????pub?fn?new(x:?usize)?->?Self?{
????????Foo?{?x?}
????}

????pub?fn?x<'a>(&'a?self)?->?&'a?usize?{
????????&self.x
????}
}

91. Load JSON file into struct

json轉(zhuǎn)結(jié)構(gòu)體

package?main

import?"fmt"
import?"io/ioutil"
import?"encoding/json"

func?readJSONFile()?error?{
?var?x?Person

?buffer,?err?:=?ioutil.ReadFile(filename)
?if?err?!=?nil?{
??return?err
?}
?err?=?json.Unmarshal(buffer,?&x)
?if?err?!=?nil?{
??return?err
?}

?fmt.Println(x)
?return?nil
}

func?main()?{
?err?:=?readJSONFile()
?if?err?!=?nil?{
??panic(err)
?}
}

type?Person?struct?{
?FirstName?string
?Age???????int
}

const?filename?=?"/tmp/data.json"

func?init()?{
?err?:=?ioutil.WriteFile(filename,?[]byte(`
??{
???"FirstName":"Napoléon",
???"Age":?51?
??}`
),?0644)
?if?err?!=?nil?{
??panic(err)
?}
}

{Napoléon 51}

or

package?main

import?(
?"encoding/json"
?"fmt"
?"io/ioutil"
?"os"
)

func?readJSONFile()?error?{
?var?x?Person

?r,?err?:=?os.Open(filename)
?if?err?!=?nil?{
??return?err
?}
?decoder?:=?json.NewDecoder(r)
?err?=?decoder.Decode(&x)
?if?err?!=?nil?{
??return?err
?}

?fmt.Println(x)
?return?nil
}

func?main()?{
?err?:=?readJSONFile()
?if?err?!=?nil?{
??panic(err)
?}
}

type?Person?struct?{
?FirstName?string
?Age???????int
}

const?filename?=?"/tmp/data.json"

func?init()?{
?err?:=?ioutil.WriteFile(filename,?[]byte(`
??{
???"FirstName":"Napoléon",
???"Age":?51?
??}`
),?0644)
?if?err?!=?nil?{
??panic(err)
?}
}

{Napoléon 51}


#[macro_use]?extern?crate?serde_derive;
extern?crate?serde_json;
use?std::fs::File;
let?x?=?::serde_json::from_reader(File::open("data.json")?)?;

92. Save object into JSON file

將json對象寫入文件

package?main

import?"fmt"
import?"io/ioutil"
import?"encoding/json"

func?writeJSONFile()?error?{
?x?:=?Person{
??FirstName:?"Napoléon",
??Age:???????51,
?}

?buffer,?err?:=?json.MarshalIndent(x,?"",?"??")
?if?err?!=?nil?{
??return?err
?}
?return?ioutil.WriteFile(filename,?buffer,?0644)
}

func?main()?{
?err?:=?writeJSONFile()
?if?err?!=?nil?{
??panic(err)
?}
?fmt.Println("Done.")
}

type?Person?struct?{
?FirstName?string
?Age???????int
}

const?filename?=?"/tmp/data.json"

json.MarshalIndent is more human-readable than json.Marshal.

Done.


extern?crate?serde_json;
#[macro_use]?extern?crate?serde_derive;

use?std::fs::File;
::serde_json::to_writer(&File::create("data.json")?,?&x)?

93. Pass a runnable procedure as parameter

Implement procedure control which receives one parameter f, and runs f.

以函數(shù)作為參數(shù)

package?main

import?"fmt"

func?main()?{
?control(greet)
}

func?control(f?func())?{
?fmt.Println("Before?f")
?f()
?fmt.Println("After?f")
}

func?greet()?{
?fmt.Println("Hello,?developers")
}

Go supports first class functions, higher-order functions, user-defined function types, function literals, and closures.

Before?f
Hello,?developers
After?f

fn?control(f:?impl?Fn())?{
????f();
}

fn?hello()?{
????println!("Hello,");
}

fn?main()?{
????control(hello);
????control(||?{?println!("Is?there?anybody?in?there?");?});
}

Hello,
Is?there?anybody?in?there?

94. Print type of variable

打印變量的類型

package?main

import?(
?"fmt"
?"os"
?"reflect"
)

func?main()?{
?var?x?interface{}

?x?=?"Hello"
?fmt.Println(reflect.TypeOf(x))

?x?=?4
?fmt.Println(reflect.TypeOf(x))

?x?=?os.NewFile(0777,?"foobar.txt")
?fmt.Println(reflect.TypeOf(x))
}

string
int
*os.File

or

package?main

import?(
?"fmt"
?"os"
)

func?main()?{
?var?x?interface{}

?x?=?"Hello"
?fmt.Printf("%T",?x)
?fmt.Println()

?x?=?4
?fmt.Printf("%T",?x)
?fmt.Println()

?x?=?os.NewFile(0777,?"foobar.txt")
?fmt.Printf("%T",?x)
?fmt.Println()
}
string
int
*os.File

#![feature(core_intrinsics)]

fn?type_of<T>(_:?&T)?->?String?{
????format!("{}",?std::intrinsics::type_name::<T>())
}

fn?main()?{
????let?x:?i32?=?1;
????println!("{}",?type_of(&x));
}

i32


95. Get file size

獲取文件的大小

package?main

import?(
?"fmt"
?"io/ioutil"
?"os"
)

func?main()?{
?err?:=?printSize("file.txt")
?if?err?!=?nil?{
??panic(err)
?}
}

func?printSize(path?string)?error?{
?info,?err?:=?os.Stat(path)
?if?err?!=?nil?{
??return?err
?}
?x?:=?info.Size()

?fmt.Println(x)
?return?nil
}

func?init()?{
?//?The?file?will?only?contains?the?characters?"Hello",?no?newlines.
?buffer?:=?[]byte("Hello")
?err?:=?ioutil.WriteFile("file.txt",?buffer,?0644)
?if?err?!=?nil?{
??panic(err)
?}
}

5


use?std::fs;

fn?filesize(path:?&str)?->?Result<u64,?std::io::Error>?{
????let?x?=?fs::metadata(path)?.len();
????Ok(x)
}

fn?main()?{
????let?path?=?"/etc/hosts";
????let?x?=?filesize(path);
????println!("{}:?{:?}?bytes",?path,?x.unwrap());
}

/etc/hosts: 150 bytes

or

use?std::path::Path;

fn?filesize(path:?&std::path::Path)?->?Result<u64,?std::io::Error>?{
????let?x?=?path.metadata()?.len();
????Ok(x)
}

fn?main()?{
????let?path?=?Path::new("/etc/hosts");
????let?x?=?filesize(path);
????println!("{:?}:?{:?}?bytes",?path,?x.unwrap());
}

"/etc/hosts": 150 bytes


96. Check string prefix

Set boolean b to true if string s starts with prefix prefix, false otherwise.

檢查兩個字符串前綴是否一致

package?main

import?(
?"fmt"
?"strings"
)

func?check(s,?prefix?string)?{

?b?:=?strings.HasPrefix(s,?prefix)

?if?b?{
??fmt.Println(s,?"starts?with",?prefix)
?}?else?{
??fmt.Println(s,?"doesn't?start?with",?prefix)
?}
}

func?main()?{
?check("bar",?"foo")
?check("foobar",?"foo")
}

bar?doesn't?start?with?foo
foobar?starts?with?foo

fn?main()?{
????let?s?=?"bananas";
????let?prefix?=?"bana";

????let?b?=?s.starts_with(prefix);

????println!("{:?}",?b);
}

true


97. Check string suffix

Set boolean b to true if string s ends with string suffix, false otherwise.

檢查字符串后綴

package?main

import?(
?"fmt"
?"strings"
)

func?check(s,?suffix?string)?{

?b?:=?strings.HasSuffix(s,?suffix)

?if?b?{
??fmt.Println(s,?"ends?with",?suffix)
?}?else?{
??fmt.Println(s,?"doesn't?end?with",?suffix)
?}
}

func?main()?{
?check("foo",?"bar")
?check("foobar",?"bar")
}
foo?doesn't?end?with?bar
foobar?ends?with?bar

fn?main()?{
????let?s?=?"bananas";
????let?suffix?=?"nas";

????let?b?=?s.ends_with(suffix);

????println!("{:?}",?b);
}

true


98. Epoch seconds to date object

Convert a timestamp ts (number of seconds in epoch-time) to a date with time d. E.g. 0 -> 1970-01-01 00:00:00

時間戳轉(zhuǎn)日期

package?main

import?(
?"fmt"
?"time"
)

func?main()?{
?ts?:=?int64(1451606400)
?d?:=?time.Unix(ts,?0)

?fmt.Println(d)
}

2016-01-01 00:00:00 +0000 UTC


extern?crate?chrono;
use?chrono::prelude::*;

fn?main()?{
????let?ts?=?1451606400;
????let?d?=?NaiveDateTime::from_timestamp(ts,?0);
????println!("{}",?d);
}

2016-01-01 00:00:00


99. Format date YYYY-MM-DD

Assign to string x the value of fields (year, month, day) of date d, in format YYYY-MM-DD.

時間格式轉(zhuǎn)換

package?main

import?(
?"fmt"
?"time"
)

func?main()?{
?d?:=?time.Now()
?x?:=?d.Format("2006-01-02")
?fmt.Println(x)

?//?The?output?may?be?"2009-11-10"?because?the?Playground's?clock?is?fixed?in?the?past.
}

2009-11-10


extern?crate?chrono;

use?chrono::prelude::*;

fn?main()?{
????println!("{}",?Utc::today().format("%Y-%m-%d"))
}

2021-07-17


100. Sort by a comparator

Sort elements of array-like collection items, using a comparator c.

根據(jù)某個字段排序

package?main

import?"fmt"
import?"sort"

type?Item?struct?{
?label?string
?p?????int
?lang??string
}

//?c?returns?true?if?x?is?"inferior?to"?y?(in?a?custom?way)
func?c(x,?y?Item)?bool?{
?return?x.p?<?y.p
}

type?ItemCSorter?[]Item

func?(s?ItemCSorter)?Len()?int???????????{?return?len(s)?}
func?(s?ItemCSorter)?Less(i,?j?int)?bool?{?return?c(s[i],?s[j])?}
func?(s?ItemCSorter)?Swap(i,?j?int)??????{?s[i],?s[j]?=?s[j],?s[i]?}

func?sortItems(items?[]Item)?{
?sorter?:=?ItemCSorter(items)
?sort.Sort(sorter)
}

func?main()?{
?items?:=?[]Item{
??{"twelve",?12,?"english"},
??{"six",?6,?"english"},
??{"eleven",?11,?"english"},
??{"zero",?0,?"english"},
??{"two",?2,?"english"},
?}
?fmt.Println("Unsorted:?",?items)
?sortItems(items)
?fmt.Println("Sorted:?",?items)
}

c has type func(Item, Item) bool.

Unsorted:??[{twelve?12?english}?{six?6?english}?{eleven?11?english}?{zero?0?english}?{two?2?english}]
Sorted:??[{zero?0?english}?{two?2?english}?{six?6?english}?{eleven?11?english}?{twelve?12?english}]

or

package?main

import?"fmt"
import?"sort"

type?Item?struct?{
?label?string
?p?????int
?lang??string
}

type?ItemsSorter?struct?{
?items?[]Item
?c?????func(x,?y?Item)?bool
}

func?(s?ItemsSorter)?Len()?int???????????{?return?len(s.items)?}
func?(s?ItemsSorter)?Less(i,?j?int)?bool?{?return?s.c(s.items[i],?s.items[j])?}
func?(s?ItemsSorter)?Swap(i,?j?int)??????{?s.items[i],?s.items[j]?=?s.items[j],?s.items[i]?}

func?sortItems(items?[]Item,?c?func(x,?y?Item)?bool)?{
?sorter?:=?ItemsSorter{
??items,
??c,
?}
?sort.Sort(sorter)
}

func?main()?{
?items?:=?[]Item{
??{"twelve",?12,?"english"},
??{"six",?6,?"english"},
??{"eleven",?11,?"english"},
??{"zero",?0,?"english"},
??{"two",?2,?"english"},
?}
?fmt.Println("Unsorted:?",?items)

?c?:=?func(x,?y?Item)?bool?{
??return?x.p?<?y.p
?}
?sortItems(items,?c)

?fmt.Println("Sorted:?",?items)
}

ItemsSorter contains c, which can be any comparator decided at runtime.

Unsorted:??[{twelve?12?english}?{six?6?english}?{eleven?11?english}?{zero?0?english}?{two?2?english}]
Sorted:??[{zero?0?english}?{two?2?english}?{six?6?english}?{eleven?11?english}?{twelve?12?english}]

or

package?main

import?"fmt"
import?"sort"

type?Item?struct?{
?label?string
?p?????int
?lang??string
}

//?c?returns?true?if?x?is?"inferior?to"?y?(in?a?custom?way)
func?c(x,?y?Item)?bool?{
?return?x.p?<?y.p
}

func?main()?{
?items?:=?[]Item{
??{"twelve",?12,?"english"},
??{"six",?6,?"english"},
??{"eleven",?11,?"english"},
??{"zero",?0,?"english"},
??{"two",?2,?"english"},
?}
?fmt.Println("Unsorted:?",?items)
?
?sort.Slice(items,?func(i,?j?int)?bool?{
??return?c(items[i],?items[j])
?})
?
?fmt.Println("Sorted:?",?items)
}

Since Go 1.8, a single func parameter is sufficient to sort a slice.

Unsorted:??[{twelve?12?english}?{six?6?english}?{eleven?11?english}?{zero?0?english}?{two?2?english}]
Sorted:??[{zero?0?english}?{two?2?english}?{six?6?english}?{eleven?11?english}?{twelve?12?english}]

fn?main()?{
????let?mut?items?=?[1,?7,?5,?2,?3];
????items.sort_by(i32::cmp);
????println!("{:?}",?items);
}

[1, 2, 3, 5, 7]


參考資料

[1]

Rust vs Go 2023: https://arkiana.com/rust-vs-go/

本文由 mdnice 多平臺發(fā)布文章來源地址http://www.zghlxwxcb.cn/news/detail-606324.html

到了這里,關(guān)于Rust vs Go:常用語法對比(五)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關(guān)文章

  • Rust vs Go:常用語法對比(七)

    Rust vs Go:常用語法對比(七)

    題圖來自 Go vs Rust: Which will be the top pick in programming? [1] 121. UDP listen and read Listen UDP traffic on port p and read 1024 bytes into buffer b. 聽端口p上的UDP流量,并將1024字節(jié)讀入緩沖區(qū)b。 122. Declare enumeration Create an enumerated type Suit with 4 possible values SPADES, HEARTS, DIAMONDS, CLUBS. 聲明枚舉值 Hearts

    2024年02月15日
    瀏覽(20)
  • Rust vs Go:常用語法對比(五)

    Rust vs Go:常用語法對比(五)

    題圖來自 Rust vs Go 2023 [1] 81. Round floating point number to integer Declare integer y and initialize it with the rounded value of floating point number x . Ties (when the fractional part of x is exactly .5) must be rounded up (to positive infinity). 按規(guī)則取整 2.71828 3 82. Count substring occurrences 統(tǒng)計子字符串出現(xiàn)次數(shù) 1 Disjoint ma

    2024年02月15日
    瀏覽(21)
  • Rust vs Go:常用語法對比(十)

    Rust vs Go:常用語法對比(十)

    題圖來自 Rust vs. Golang: Which One is Better? [1] 182. Quine program Output the source of the program. 輸出程序的源代碼 輸出: 另一種寫法: //go:embed 入門 [2] Quine 是一種可以輸出自身源碼的程序。利用 go:embed 我們可以輕松實現(xiàn) quine 程序: 輸出: or 輸出: fn main(){print!(\\\"{},{0:?})}}\\\",\\\"fn main(){pri

    2024年02月15日
    瀏覽(23)
  • Rust vs Go:常用語法對比(十一)

    Rust vs Go:常用語法對比(十一)

    題目來自 Rust Vs Go: Which Language Is Better For Developing High-Performance Applications? [1] 202. Sum of squares Calculate the sum of squares s of data, an array of floating point values. 計算平方和 +1.094200e+000 32.25 205. Get an environment variable Read an environment variable with the name \\\"FOO\\\" and assign it to the string variable foo. If i

    2024年02月15日
    瀏覽(19)
  • Rust vs Go:常用語法對比(十二)

    Rust vs Go:常用語法對比(十二)

    題圖來自 Rust vs Go in 2023 [1] 221. Remove all non-digits characters Create string t from string s, keeping only digit characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. 刪除所有非數(shù)字字符 168 [src/main.rs:7] t = \\\"14\\\" 222. Find first index of an element in list Set i to the first index in list items at which the element x can be found, or -1 if items doe

    2024年02月15日
    瀏覽(16)
  • Rust vs Go:常用語法對比(三)

    Rust vs Go:常用語法對比(三)

    題圖來自 When to use Rust and when to use Go [1] 41. Reverse a string 反轉(zhuǎn)字符串 輸出 or 輸出 ? roma tis r?lod müspi mérol 42. Continue outer loop Print each item v of list a which in not contained in list b. For this, write an outer loop to iterate on a and an inner loop to iterate on b. 打印列表a中不包含在列表b中的每個項目

    2024年02月16日
    瀏覽(23)
  • Rust vs Go:常用語法對比(六)

    Rust vs Go:常用語法對比(六)

    題圖來自 [1] 101. Load from HTTP GET request into a string Make an HTTP request with method GET to URL u, then store the body of the response in string s. 發(fā)起http請求 res has type *http.Response. buffer has type []byte. It is idiomatic and strongly recommended to check errors at each step. GET response: 200 Hello Inigo Montoya or or 102. Load from H

    2024年02月15日
    瀏覽(25)
  • Java VS Go 還在糾結(jié)怎么選嗎,(資深后端4000字帶你深度對比)

    Java VS Go 還在糾結(jié)怎么選嗎,(資深后端4000字帶你深度對比)

    今天我們來聊一下Go 和Java,本篇文章主要是想給對后臺開發(fā)的初學者和有意向選擇Go語言的有經(jīng)驗的程序員一些建議,希望能幫助各位自上而下的來了解一下Java和Go的全貌。 作為一個多年的Java后端開發(fā),用的時間久了就會發(fā)現(xiàn)Java語言一些問題,所謂婚前風花雪月,婚后柴米

    2024年02月04日
    瀏覽(26)
  • 【字節(jié)跳動青訓營】后端筆記整理-1 | Go語言入門指南:基礎語法和常用特性解析

    【字節(jié)跳動青訓營】后端筆記整理-1 | Go語言入門指南:基礎語法和常用特性解析

    **本人是第六屆字節(jié)跳動青訓營(后端組)的成員。本文由博主本人整理自該營的日常學習實踐,首發(fā)于稀土掘金:??Go語言入門指南:基礎語法和常用特性解析 | 青訓營 本文主要梳理自 第六屆字節(jié)跳動青訓營(后端組)-Go語言原理與實踐第一節(jié)(王克純老師主講) 。同時

    2024年02月13日
    瀏覽(24)
  • 【Rust日報】2023-02-14 Rust GUI 框架對比: Tauri vs Iced vs egui

    【Rust日報】2023-02-14 Rust GUI 框架對比: Tauri vs Iced vs egui

    Rust GUI 框架對比: Tauri vs Iced vs egui Tauri:使用系統(tǒng)的 webview 來渲染 HTML/JS 的前端。你可以選擇任何前端框架。后臺是用Rust編寫的,可以通過內(nèi)置的方法與前臺通信。 Iced: 受 Elm 啟發(fā)的(響應式)GUI庫。在桌面上使用 wgpu 進行渲染;實驗性的web后端創(chuàng)建DOM進行渲染。所有代碼

    2024年02月02日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包