mastouille.fr est l'un des nombreux serveurs Mastodon indépendants que vous pouvez utiliser pour participer au fédiverse.
Mastouille est une instance Mastodon durable, ouverte, et hébergée en France.

Administré par :

Statistiques du serveur :

568
comptes actifs

#RustChallenge

0 message0 participant0 message aujourd’hui

✨ Rust challenge explained

Ans: C. It never reaches the prints statement. It goes in an infinite loop 😲

But, how? Let's break down

👉 End value is 0xfffffff which is 268435455.0. it is not very important here. So let's remove it and simplify the code with a constant 20,000,000.0 end value

👉 `f` value stops increasing(round upped) after value 16777216.0 when you add just 1.0 🤯

👉 To know why,you have to understand how the f32 is stored. Float is not stored as simple as integer

👉 f32 uses IEEE 754 format to store the value

👉 Larger f32 number loses accuracy

In a 32 bit float (any language not just rust):

👉 1st bit is used for sign identification (positive or negative)
👉 Following 8 bits used for exponent
👉 Next 23 bits used for storing mantissa 🙃

You can see more here, how it is calculated
youtu.be/8afbTaA-gOQ

(Continued 👇)

#RustChallenge without semicolons it's funnier;
Find what this algo is for!

let f = |mut x: u32, mut y: u32| std::iter::once(((), 0)).chain((1..).map_while(|g| Some(((x,y)=((x%2==0).then_some(x>>1)?,(y%2==0).then_some(y>>1)?), g)))).last().map(|(_, g)| (|z: &mut u32| while *z&1==0 {*z /= 2}, g)).map(|(h, g)| (while x != 0 {match (h(&mut x), h(&mut y), x.cmp(&y)).2 {std::cmp::Ordering::Less => y = (y-x)/2, _ => x = (x-y)/2}}, y<<g).1).unwrap();

Hint: use irust and cargo fmt

✨ Underscore expressions in Rust

Underscore `_`, a reserved identifier - Anything passed to into it will be ignored(But careful, it is not dropped)

👉 Underscore doesnt copy,move or borrow the value

👉 In the following code, we create String instance 'x'.We pass it to the underscore which does nothing than ignoring whatever we give. It does not move the ownership.
Ownership still remains with 'x' 😱

let x = String::new("Kernel");
let _ = x;

👉 Then what is the use?

👉 It will be useful in situation, when you are not interested in a certain value and ignore it.

For example:
fn main(){
let x = (21, "Parker");
let (age, _) = x; //Ignore the name, only want age from the tuple.
println!("age: {}", age);
}

Resources:
runrust.miraheze.org/wiki/Unde

Underscore is not exactly "throw away" but rather it is exactly "don't bind to variable:
news.ycombinator.com/item?id=2
reddit.com/r/rust/comments/96z