An Interest In:
Web News this Week
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
Operator "as" in Rust / Operador "as" en Rust
Main code:
fn main() { let solana_mining_amount: f64 = 0.00001274; // float let solana_mining_amount_int: u64 = solana_mining_amount.floor() as u64; // int println!("{}", solana_mining_amount); // 0.00001274 println!("{}", solana_mining_amount_int); // 0 let unsigned_integer : u64 = 6; let signed_integer : i64 = unsigned_integer as i64; println!("{}", signed_integer);}
The as
operator has as its main function to transform the types of variables, to other types of variables. Strictly speaking, we could assign, on a new line, a change of type to a variable. In the event that the variable is not float, we can use it as follows:
let unsigned_integer : u64 = 6;//unsignedlet signed_integer : i64 = unsigned_integer as i64;//signedprintln!("{}", signed_integer);//6
But when we need to change the assignment type to a float variable, we will use the as operator as follows:
let solana_mining_amount: f64 = 0.00001274; // floatlet solana_mining_amount_int: u64 = solana_mining_amount.floor() as u64; // int
In let solana_mining_amount_int
you specify that you want to switch to u64. Then the variable to change solana_mining_amount
is specified with .floor()
since when dealing with float variables we are going to need the base of the number, in this case 0 to then be able to use the as
operator and assign the transformation of the variable.
Cdigo principal:
fn main() { let solana_mining_amount: f64 = 0.00001274; // float let solana_mining_amount_int: u64 = solana_mining_amount.floor() as u64; // int println!("{}", solana_mining_amount); // 0.00001274 println!("{}", solana_mining_amount_int); // 0 let unsigned_integer : u64 = 6; let signed_integer : i64 = unsigned_integer as i64; println!("{}", signed_integer);}
El operador as
tiene como principal funcin transformar los tipos de variables, a otros tipos de variables. En sentido estricto, nosotros podramos asignar, en una nueva linea, un cambio de tipo a una variable. En el caso de que la variable no sea float, podremos utilizarlo de la siguiente manera:
let unsigned_integer : u64 = 6;//unsignedlet signed_integer : i64 = unsigned_integer as i64;//signedprintln!("{}", signed_integer);//6
Pero cuando necesitamos cambiar de tipo de asignacin a una variable float, utilizaremos el operador as de la siguiente manera:
let solana_mining_amount: f64 = 0.00001274; // floatlet solana_mining_amount_int: u64 = solana_mining_amount.floor() as u64; // int
En let solana_mining_amount_int
se especifica que se quiere cambiar a u64. Luego se especifica la variable a cambiar solana_mining_amount
con .floor()
ya que al tartar con variables float vamos a necesitar de la base del numero, en este caso 0 para luego poder utilizar el operador as
y asignar la transformacin de la variable.
Original Link: https://dev.to/mateolafalce/operador-as-en-rust-5k1
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To