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

Rust vs Go:常用語法對(duì)比(四)

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


Rust vs Go:常用語法對(duì)比(四),后端

題圖來自 Go vs. Rust performance comparison: The basics


61. Get current date

獲取當(dāng)前時(shí)間

package?main

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

func?main()?{
?d?:=?time.Now()
?fmt.Println("Now?is",?d)
?//?The?Playground?has?a?special?sandbox,?so?you?may?get?a?Time?value?fixed?in?the?past.
}

Now is 2009-11-10 23:00:00 +0000 UTC m=+0.000000001


extern?crate?time;
let?d?=?time::now();

or

use?std::time::SystemTime;

fn?main()?{
????let?d?=?SystemTime::now();
????println!("{:?}",?d);
}

SystemTime { tv_sec: 1526318418, tv_nsec: 699329521 }


62. Find substring position

字符串查找

查找子字符串位置

package?main

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

func?main()?{
?x?:=?"été?chaud"

?{
??y?:=?"chaud"
??i?:=?strings.Index(x,?y)
??fmt.Println(i)
?}

?{
??y?:=?"froid"
??i?:=?strings.Index(x,?y)
??fmt.Println(i)
?}
}

i is the byte index of y in x, not the character (rune) index. i will be -1 if y is not found in x.

6
-1

fn?main()?{
????let?x?=?"été?chaud";
????
????let?y?=?"chaud";
????let?i?=?x.find(y);
????println!("{:?}",?i);
????
????let?y?=?"froid";
????let?i?=?x.find(y);
????println!("{:?}",?i);
}
Some(6)
None

63. Replace fragment of a string

替換字符串片段

package?main

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

func?main()?{
?x?:=?"oink?oink?oink"
?y?:=?"oink"
?z?:=?"moo"
?x2?:=?strings.Replace(x,?y,?z,?-1)
?fmt.Println(x2)
}

moo moo moo


fn?main()?{
????let?x?=?"lorem?ipsum?dolor?lorem?ipsum";
????let?y?=?"lorem";
????let?z?=?"LOREM";

????let?x2?=?x.replace(&y,?&z);
????
????println!("{}",?x2);
}

LOREM ipsum dolor LOREM ipsum


64. Big integer : value 3 power 247

超大整數(shù)

package?main

import?"fmt"
import?"math/big"

func?main()?{
?x?:=?new(big.Int)
?x.Exp(big.NewInt(3),?big.NewInt(247),?nil)
?fmt.Println(x)
}

7062361041362837614435796717454722507454089864783271756927542774477268334591598635421519542453366332460075473278915787


extern?crate?num;
use?num::bigint::ToBigInt;

fn?main()?{
????let?a?=?3.to_bigint().unwrap();
????let?x?=?num::pow(a,?247);
????println!("{}",?x)
}

7062361041362837614435796717454722507454089864783271756927542774477268334591598635421519542453366332460075473278915787


65. Format decimal number

格式化十進(jìn)制數(shù)

package?main

import?"fmt"

func?main()?{
?x?:=?0.15625
?s?:=?fmt.Sprintf("%.1f%%",?100.0*x)
?fmt.Println(s)
}

15.6%


fn?main()?{
????let?x?=?0.15625f64;
????let?s?=?format!("{:.1}%",?100.0?*?x);
????
????println!("{}",?s);
}

15.6%


66. Big integer exponentiation

大整數(shù)冪運(yùn)算

package?main

import?"fmt"
import?"math/big"

func?exp(x?*big.Int,?n?int)?*big.Int?{
?nb?:=?big.NewInt(int64(n))
?var?z?big.Int
?z.Exp(x,?nb,?nil)
?return?&z
}

func?main()?{
?x?:=?big.NewInt(3)
?n?:=?5
?z?:=?exp(x,?n)
?fmt.Println(z)
}

243


extern?crate?num;

use?num::bigint::BigInt;

fn?main()?{
????let?x?=?BigInt::parse_bytes(b"600000000000",?10).unwrap();
????let?n?=?42%

67. Binomial coefficient "n choose k"

Calculate binom(n, k) = n! / (k! * (n-k)!). Use an integer type able to handle huge numbers.

二項(xiàng)式系數(shù)“n選擇k”

package?main

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

func?main()?{
?z?:=?new(big.Int)
?
?z.Binomial(4,?2)
?fmt.Println(z)
?
?z.Binomial(133,?71)
?fmt.Println(z)
}

6
555687036928510235891585199545206017600

extern?crate?num;

use?num::bigint::BigInt;
use?num::bigint::ToBigInt;
use?num::traits::One;

fn?binom(n:?u64,?k:?u64)?->?BigInt?{
????let?mut?res?=?BigInt::one();
????for?i?in?0..k?{
????????res?=?(res?*?(n?-?i).to_bigint().unwrap())?/
??????????????(i?+?1).to_bigint().unwrap();
????}
????res
}

fn?main()?{
????let?n?=?133;
????let?k?=?71;

????println!("{}",?binom(n,?k));
}

555687036928510235891585199545206017600


68. Create a bitset

創(chuàng)建位集合

package?main

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

func?main()?{
?var?x?*big.Int?=?new(big.Int)

?x.SetBit(x,?42,?1)

?for?_,?y?:=?range?[]int{13,?42}?{
??fmt.Println("x?has?bit",?y,?"set?to",?x.Bit(y))
?}
}
x?has?bit?13?set?to?0
x?has?bit?42?set?to?1

or

package?main

import?(
?"fmt"
)

const?n?=?1024

func?main()?{
?x?:=?make([]bool,?n)

?x[42]?=?true

?for?_,?y?:=?range?[]int{13,?42}?{
??fmt.Println("x?has?bit",?y,?"set?to",?x[y])
?}
}
x?has?bit?13?set?to?false
x?has?bit?42?set?to?true

or

package?main

import?(
?"fmt"
)

func?main()?{
?const?n?=?1024

?x?:=?NewBitset(n)

?x.SetBit(13)
?x.SetBit(42)
?x.ClearBit(13)

?for?_,?y?:=?range?[]int{13,?42}?{
??fmt.Println("x?has?bit",?y,?"set?to",?x.GetBit(y))
?}
}

type?Bitset?[]uint64

func?NewBitset(n?int)?Bitset?{
?return?make(Bitset,?(n+63)/64)
}

func?(b?Bitset)?GetBit(index?int)?bool?{
?pos?:=?index?/?64
?j?:=?index?%?64
?return?(b[pos]?&?(uint64(1)?<<?j))?!=?0
}

func?(b?Bitset)?SetBit(index?int)?{
?pos?:=?index?/?64
?j?:=?index?%?64
?b[pos]?|=?(uint64(1)?<<?j)
}

func?(b?Bitset)?ClearBit(index?int)?{
?pos?:=?index?/?64
?j?:=?index?%?64
?b[pos]?^=?(uint64(1)?<<?j)
}

x?has?bit?13?set?to?false
x?has?bit?42?set?to?true

fn?main()?{
????let?n?=?20;

????let?mut?x?=?vec![false;?n];

????x[3]?=?true;
????println!("{:?}",?x);
}

[false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]


69. Seed random generator

Use seed s to initialize a random generator.

If s is constant, the generator output will be the same each time the program runs. If s is based on the current value of the system clock, the generator output will be different each time.

隨機(jī)種子生成器

package?main

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

func?main()?{
?var?s?int64?=?42
?rand.Seed(s)
?fmt.Println(rand.Int())
}

3440579354231278675

or

package?main

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

func?main()?{
?var?s?int64?=?42
?r?:=?rand.New(rand.NewSource(s))
?fmt.Println(r.Int())
}

3440579354231278675


use?rand::{Rng,?SeedableRng,?rngs::StdRng};

fn?main()?{
????let?s?=?32;
????let?mut?rng?=?StdRng::seed_from_u64(s);
????
????println!("{:?}",?rng.gen::<f32>());
}

0.35038823


70. Use clock as random generator seed

Get the current datetime and provide it as a seed to a random generator. The generator sequence will be different at each run.

使用時(shí)鐘作為隨機(jī)生成器的種子

package?main

import?(
?"fmt"
?"math/rand"
?"time"
)

func?main()?{
?rand.Seed(time.Now().UnixNano())
?//?Well,?the?playground?date?is?actually?fixed?in?the?past,?and?the
?//?output?is?cached.
?//?But?if?you?run?this?on?your?workstation,?the?output?will?vary.
?fmt.Println(rand.Intn(999))
}

524

or

package?main

import?(
?"fmt"
?"math/rand"
?"time"
)

func?main()?{
?r?:=?rand.New(rand.NewSource(time.Now().UnixNano()))
?//?Well,?the?playground?date?is?actually?fixed?in?the?past,?and?the
?//?output?is?cached.
?//?But?if?you?run?this?on?your?workstation,?the?output?will?vary.
?fmt.Println(r.Intn(999))
}

524


use?rand::{Rng,?SeedableRng,?rngs::StdRng};
use?std::time::SystemTime;

fn?main()?{
????let?d?=?SystemTime::now()
????????.duration_since(SystemTime::UNIX_EPOCH)
????????.expect("Duration?since?UNIX_EPOCH?failed");
????let?mut?rng?=?StdRng::seed_from_u64(d.as_secs());
????
????println!("{:?}",?rng.gen::<f32>());
}

0.7326781


71. Echo program implementation

Basic implementation of the Echo program: Print all arguments except the program name, separated by space, followed by newline.
The idiom demonstrates how to skip the first argument if necessary, concatenate arguments as strings, append newline and print it to stdout.

實(shí)現(xiàn) Echo 程序

package?main
import?"fmt"
import?"os"
import?"strings"
func?main()?{
????fmt.Println(strings.Join(os.Args[1:],?"?"))
}

use?std::env;

fn?main()?{
????println!("{}",?env::args().skip(1).collect::<Vec<_>>().join("?"));
}

or

use?itertools::Itertools;
println!("{}",?std::env::args().skip(1).format("?"));

74. Compute GCD

Compute the greatest common divisor x of big integers a and b. Use an integer type able to handle huge numbers.

計(jì)算大整數(shù)a和b的最大公約數(shù)x。使用能夠處理大數(shù)的整數(shù)類型。

package?main

import?"fmt"
import?"math/big"

func?main()?{
?a,?b,?x?:=?new(big.Int),?new(big.Int),?new(big.Int)
?a.SetString("6000000000000",?10)
?b.SetString("9000000000000",?10)
?x.GCD(nil,?nil,?a,?b)
?fmt.Println(x)
}

3000000000000


extern?crate?num;

use?num::Integer;
use?num::bigint::BigInt;

fn?main()?{
????let?a?=?BigInt::parse_bytes(b"6000000000000",?10).unwrap();
????let?b?=?BigInt::parse_bytes(b"9000000000000",?10).unwrap();
????
????let?x?=?a.gcd(&b);
?
????println!("{}",?x);
}

3000000000000


75. Compute LCM

計(jì)算大整數(shù)a和b的最小公倍數(shù)x。使用能夠處理大數(shù)的整數(shù)類型。

Compute the least common multiple x of big integers a and b. Use an integer type able to handle huge numbers.

package?main

import?"fmt"
import?"math/big"

func?main()?{
?a,?b,?gcd,?x?:=?new(big.Int),?new(big.Int),?new(big.Int),?new(big.Int)
?a.SetString("6000000000000",?10)
?b.SetString("9000000000000",?10)
?gcd.GCD(nil,?nil,?a,?b)
?x.Div(a,?gcd).Mul(x,?b)
?fmt.Println(x)
}

18000000000000


extern?crate?num;

use?num::bigint::BigInt;
use?num::Integer;

fn?main()?{
????let?a?=?BigInt::parse_bytes(b"6000000000000",?10).unwrap();
????let?b?=?BigInt::parse_bytes(b"9000000000000",?10).unwrap();
????let?x?=?a.lcm(&b);
????println!("x?=?{}",?x);
}

x = 18000000000000


76. Binary digits from an integer

Create the string s of integer x written in base 2.
E.g. 13 -> "1101"

將十進(jìn)制整數(shù)轉(zhuǎn)換為二進(jìn)制數(shù)字

package?main

import?"fmt"
import?"strconv"

func?main()?{
?x?:=?int64(13)
?s?:=?strconv.FormatInt(x,?2)

?fmt.Println(s)
}

1101

or

package?main

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

func?main()?{
?x?:=?big.NewInt(13)
?s?:=?fmt.Sprintf("%b",?x)

?fmt.Println(s)
}

1101


fn?main()?{
????let?x?=?13;
????let?s?=?format!("{:b}",?x);
????
????println!("{}",?s);
}

1101


77. SComplex number

Declare a complex x and initialize it with value (3i - 2). Then multiply it by i.

復(fù)數(shù)

package?main

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

func?main()?{
?x?:=?3i?-?2
?x?*=?1i

?fmt.Println(x)
?fmt.Print(reflect.TypeOf(x))
}

(-3-2i)
complex128

extern?crate?num;
use?num::Complex;

fn?main()?{
????let?mut?x?=?Complex::new(-2,?3);
????x?*=?Complex::i();
????println!("{}",?x);
}

-3-2i


78. "do while" loop

Execute a block once, then execute it again as long as boolean condition c is true.

循環(huán)執(zhí)行

package?main

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

func?main()?{
?for?{
??x?:=?rollDice()
??fmt.Println("Got",?x)
??if?x?==?3?{
???break
??}

?}
}

func?rollDice()?int?{
?return?1?+?rand.Intn(6)
}

Go has no do while loop, use the for loop, instead.

Got?6
Got?4
Got?6
Got?6
Got?2
Got?1
Got?2
Got?3

or

package?main

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

func?main()?{
?for?done?:=?false;?!done;?{
??x?:=?rollDice()
??fmt.Println("Got",?x)
??done?=?x?==?3
?}
}

func?rollDice()?int?{
?return?1?+?rand.Intn(6)
}
Got?6
Got?4
Got?6
Got?6
Got?2
Got?1
Got?2
Got?3

loop?{
????doStuff();
????if?!c?{?break;?}
}

Rust has no do-while loop with syntax sugar. Use loop and break.


79. Convert integer to floating point number

Declare floating point number y and initialize it with the value of integer x .

整型轉(zhuǎn)浮點(diǎn)型

聲明浮點(diǎn)數(shù)y并用整數(shù)x的值初始化它。

package?main

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

func?main()?{
?x?:=?5
?y?:=?float64(x)

?fmt.Println(y)
?fmt.Printf("%.2f\n",?y)
?fmt.Println(reflect.TypeOf(y))
}
5
5.00
float64

fn?main()?{
????let?i?=?5;
????let?f?=?i?as?f64;
????
????println!("int?{:?},?float?{:?}",?i,?f);
}
int?5,?float?5.0

80. Truncate floating point number to integer

Declare integer y and initialize it with the value of floating point number x . Ignore non-integer digits of x . Make sure to truncate towards zero: a negative x must yield the closest greater integer (not lesser).

浮點(diǎn)型轉(zhuǎn)整型

package?main

import?"fmt"

func?main()?{
?a?:=?-6.4
?b?:=?6.4
?c?:=?6.6
?fmt.Println(int(a))
?fmt.Println(int(b))
?fmt.Println(int(c))
}
-6
6
6

fn?main()?{
????let?x?=?41.59999999f64;
????let?y?=?x?as?i32;
????println!("{}",?y);
}

41


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

到了這里,關(guān)于Rust vs Go:常用語法對(duì)比(四)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

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

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

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

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

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

    題目來自 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. 計(jì)算平方和 +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:常用語法對(duì)比(十二)

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

    題圖來自 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:常用語法對(duì)比(二)

    21. Swap values 交換變量a和b的值 輸出 a: 10, b: 3 or 輸出 22. Convert string to integer 將字符串轉(zhuǎn)換為整型 or 輸出 or 輸出 or 輸出 23. Convert real number to string with 2 decimal places Given a real number x, create its string representation s with 2 decimal digits following the dot. 給定一個(gè)實(shí)數(shù),小數(shù)點(diǎn)后保留兩位小數(shù)

    2024年02月16日
    瀏覽(28)
  • Rust vs Go:常用語法對(duì)比(五)

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

    題圖來自 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)計(jì)子字符串出現(xiàn)次數(shù) 1 Disjoint ma

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

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

    題圖來自 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中的每個(gè)項(xiàng)目

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

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

    題圖來自 [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請(qǐng)求 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日
    瀏覽(26)
  • Java VS Go 還在糾結(jié)怎么選嗎,(資深后端4000字帶你深度對(duì)比)

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

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

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

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

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

    2024年02月13日
    瀏覽(24)
  • 【Rust日?qǐng)?bào)】2023-02-14 Rust GUI 框架對(duì)比: Tauri vs Iced vs egui

    【Rust日?qǐng)?bào)】2023-02-14 Rust GUI 框架對(duì)比: Tauri vs Iced vs egui

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

    2024年02月02日
    瀏覽(21)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包