简体   繁体   English

BigUint::from(24u32) 有什么作用?

[英]What does BigUint::from(24u32) do?

BigUint::from(24u32)

I understand that it is related to a big unsigned integer since it is part of a cryptography module to factor big numbers.我知道它与一个大的无符号整数有关,因为它是用于分解大数字的密码学模块的一部分。 I don't understand what the part from(24u32) does.我不明白from(24u32)的部分from(24u32)做什么的。 For example, if it is used in this context例如,如果在此上下文中使用它

let b: BigUint = (BigUint::from(24u32) * &n).sqrt() + &one;

Conversions between types in Rust are defined by the From trait which defines a function from , so that Foo::from (bar) converts the value bar to the type Foo . Rust 中类型之间的转换由From trait 定义,它定义了一个函数from ,因此Foo::from (bar)将值bar转换为类型Foo In your case, you are therefore converting 24u32 to type BigUint , but what's this 24u32 ?在你的情况,你因此被转换24u32键入BigUint ,但是这是什么24u32

Integer literals in Rust can use a suffix to specify their actual type. Rust 中的整数文字可以使用后缀来指定它们的实际类型。 If you wrote just 24 , it could be for example an i32 , a u8 or a u32 .如果你写的只是24 ,它可以是例如i32 ,一个u8u32 Most of the time the compiler is able to infer the actual type from the way the value is used, and when it can't it defaults to i32 .大多数情况下,编译器能够从值的使用方式推断出实际类型,如果不能,则默认为i32 But in your case that won't work: there is no BigUint::from<i32> but there are conversion functions for all the regular unsigned types: u8 , u16 , u32 , u64 and u128 , so the compiler doesn't know which one to use.但是,你的情况是行不通的:没有BigUint::from<i32>但也有所有的常规无符号类型转换功能: u8u16u32u64u128 ,所以编译器不知道哪个一个使用。 Adding the u32 suffix clarifies the type, so 24u32 is the value 24 with the type u32 , allowing the compiler to understand that you want BigUint::from<u32> (24) .添加u32后缀澄清的类型,所以24u32是价值24同类型u32 ,让编译器知道你是想BigUint::from<u32> (24)

The BigUint struct implements the From<u32> trait , which means that it will implement a from(u32) function. BigUint结构体实现了From<u32> trait ,这意味着它将实现一个from(u32)函数。

Implementations of the From<_> trait are used to perform a value conversion that will consume the original input. From<_> trait的实现用于执行将消耗原始输入的值转换。 In your example the BigUint struct is constructed from the 24u32 number.在您的示例中, BigUint结构是从24u32数字构造的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM