r/rustjerk • • Jan 07 '22

Someone teach him how to write bad code in Rust

Post image
1.4k Upvotes

23 comments sorted by

120

u/maroider Jan 07 '22

If the type doesn't fit, use mem::transmute so it does.

31

u/SlightlyOutOfPhase4B Jan 07 '22

That's not supported though. This doesn't compile:

fn main() {
    let x: u8 = unsafe { std::mem::transmute(usize::MAX) };
}

You get:

error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
 --> src/main.rs:2:30
  |
2 |         let x: u8 = unsafe { std::mem::transmute(usize::MAX) };
  |                              ^^^^^^^^^^^^^^^^^^^
  |
  = note: source type: `usize` (64 bits)
  = note: target type: `u8` (8 bits)

On the other hand though, this is fine for some reason:

fn main() {
    let x: u8 = usize::MAX as u8;
}

So in certain cases transmute is actually safer than other ways you could attempt to do something. The function that disregards the size of the types, that you may have been thinking of, is transmute_copy.

19

u/maroider Jan 07 '22

I was thinking more of how mem::transmute would let you shove the proverbial square peg into a round hole, rather than specifically dealing with differently-sized types. You do, however, have a point.

4

u/SlightlyOutOfPhase4B Jan 07 '22 edited Jan 07 '22

That's fair, yeah. Although there's not that many ways to use transmute incorrectly that would actually ever produce desirable results anyways, I'd note. For example:

#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
#[repr(transparent)]
struct First {
    i: i32,
}

#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
#[repr(transparent)]
struct Second {
    f: f32,
}

fn main() {
    let x = First { i: 24 };
    // This isn't useful at all
    let y: Second = unsafe { std::mem::transmute(x) };
    // Does not print `24.0` for `y.f`, of course
    println!("{:?}\n{:?}", x, y);
}

6

u/A1oso 🦀pomsky🦀 Jan 08 '22

Even better is transmute_copy. It works on types with different sizes.

5

u/Da-Blue-Guy trait Gender: Any Jun 05 '23

If the size doesn't fit, ptr::read::<T>(&val) until it does.

55

u/lmao_no_generics Jan 07 '22

The dude is going through first stages of grief. Just check his twitter in a year or so.

14

u/mrprofessor007 Jan 07 '22

Looks like someone who just got a PIP.

54

u/bascule Jan 07 '22

Lifetime problems? Why not Clone?

Clone not doing the trick? Use unsafe and turn those lifetimes into pointers!

Pointer provenance got you down? Turn those pointers into usize and back!

Miss global variables? pub static mut is a thing!

13

u/Lich_Hegemon Jan 07 '22

Pointer provenance got you down? Turn those pointers into usize and back!

Not like that, no

83

u/Ahajha1177 Jan 07 '22

Just wrap everything in unsafe, work like RAII was never invented, ignore common rust patterns, etc.

Basically, just write C, but in Rust. I think there's even a crate that lets you skirt around the borrow checker.

48

u/zepperoni-pepperoni Fn(Garbage) -> Garbage Jan 07 '22

unsafe fn main baybee!

3

u/[deleted] Jan 07 '22

That was pretty much how I initially learned Rust, minus the unsafe bit.

Clippy carried me the rest of the way.

4

u/fullouterjoin &mut self Jan 07 '22

Here is Clippy carrying you to the borrow checker https://upload.wikimedia.org/wikipedia/en/e/ea/Queen_News_Of_The_World.png

15

u/dpc_pw Jan 07 '22

Yeah, Rust sucks... for writing bad programs.

7

u/Da-Blue-Guy trait Gender: Any Oct 31 '22

rust sucks at sucking

10

u/Masaylighto Jan 07 '22

just use *mut raw pointer

6

u/olsonexi Jan 24 '22
use you_can;

#[you_can::turn_off_the_borrow_checker]
fn main() {
    unsafe {
        
        // code goes here
        
    }
}

0

u/[deleted] Jan 07 '22

[deleted]

11

u/Snakehand all comments formally proven with coq Jan 07 '22

Actually heard from a Rust programmer : I am to bad of a programmer to be writing C, therefore I chose Rust.

3

u/Follpvosten Jan 08 '22

unjerk!()

I'm literally bad at any language that isn't Rust.

3

u/Rusty_devl Jan 16 '22

I've been using c++ in the past, but moved to Rust more than one year ago. Recently I pinged a friend to look at like 5 loc in C++, just to tell me if the transposing of a matrix there is causing some aliasing issue since the Eigen documentation had some warnings on that and I was getting strange issues. It's probably good that (hopefully) nobody will ever use C++ code I wrote.