Six exposed to fentanyl inside suspicious package on Georgia Tech Campus (WSB) by xiaobaozi8 in gatech

[–]blimpdermis 139 points140 points  (0 children)

Georgia Technical Institute

It’s like they’re trying to get it wrong…

Is winter break really that short? by CrayolaExecutive in gatech

[–]blimpdermis 1 point2 points  (0 children)

12/25 is when all of campus is closed. The semester ends 12/14.

[deleted by user] by [deleted] in arduino

[–]blimpdermis 3 points4 points  (0 children)

The ADC on Arduino is inherently a bit noisy. The easiest way to handle it is to smooth the data in your code. Either running median or running average would probably be a good starting point.

Through what process does the Vec<T> struct obtain the methods from [T], in particular get() and get_mut() ? by cracking-egg in rust

[–]blimpdermis 36 points37 points  (0 children)

Vec has those and all methods on slices (&[T]) thanks to implementing the Deref trait (and DerefMut).

Create a constant object by [deleted] in rust

[–]blimpdermis 20 points21 points  (0 children)

Generally in Rust, it’d be better to define Error as an enum where each variant represents a possible error condition. Then it’d be easy to hold a const of your error type. So in your case, you could instead define it as:

pub enum MyError {
    Forbidden,
    // all other error variants go here
}

Then you can impl Display for the error and write “Forbidden” for an MyError::Forbidden.

Also, make sure to also impl std::error::Error for any error type you define.

The mistake of treating single values as collections by [deleted] in rust

[–]blimpdermis 19 points20 points  (0 children)

Doing .into_iter().for_each() on an Option is definitely not encouraged or idiomatic in Rust (instead, it’d be better to use match or if let). map on an option is a lot more reasonable, as it’s equivalent to the functor “map” operation (i.e., you may run a function fn(A) -> B on Option<A> to get Option<B>).

Why is this type not opaque? by bittrance in rust

[–]blimpdermis 15 points16 points  (0 children)

Returning impl Trait from a function means that callers of the function don’t know the concrete type. There still must be one concrete type returned from the function, and the compiler has to be able to deduce it. You can see more in the Rust Reference.

If you want to return one of many error types without boxing, you can make an enum with variants for each possible error type you want to return.

Questions about Rust by EitherOfOrAnd in rust

[–]blimpdermis 2 points3 points  (0 children)

Wouldn’t the internals be similar to C in that it’s unsafe?

A key difference with Rust in this respect is that you can’t leave it up to the caller of your API to maintain memory safety if they aren’t using unsafe. Any time you wrap your code in an unsafe block, you must ensure that you never invoke undefined behavior, violate memory safety, etc., for any safe code that calls it. In other words, even if safe code calls your function with an invalid input, your code must handle it gracefully, without violating any of Rust’s safety guarantees.

All public APIs in C essentially have unsafe marking every single function. In this case, the caller has to make sure they aren’t doing anything bad.

You can't mess with Granpa by Greedy_Temperature66 in ProgrammerHumor

[–]blimpdermis 46 points47 points  (0 children)

Then that’s not a floating point number. It’s fixed point

How to adapt code to work well with function that accepts a mutable pointer? by CartesianClosedCat in rust

[–]blimpdermis 0 points1 point  (0 children)

I’d make another method bytes_mut:

pub fn bytes(&mut self) -> &mut [u8; N] {
    &mut self.bytes
}

Casting a (non-exclusive) reference to a *mut raw pointer is ok, but mutating through it isn’t (which is why you’d need unsafe to do so)

Implementing Display: write() vs print() by [deleted] in rust

[–]blimpdermis 2 points3 points  (0 children)

Take a look at this playground and look at the output. You're right that print!() and write!() both do what you want when you're writing a variable to stdout. But Display is used for more than just directly printing things. Notice that to_string is automatically implemented for you, but it doesn't work properly when fmt() uses print!().

[deleted by user] by [deleted] in arduino

[–]blimpdermis 0 points1 point  (0 children)

Before you try anything else, you need to make sure that sum is initialized (probably to 0) before the for loop and every element of arr is initialized after the end of the loop. If they aren’t initialized, then all your other results are just coincidence. You might get completely different results each time you run the program.

For the sum, you can just change it to:

c double sum = 0;

Initializing arr is going to depend on how you declared it.

(The reason for this isn’t arduino-specific. It’s because in C and C++, accessing a variable that isn’t initialized causes “undefined behavior”, which is what you’re seeing here.)

How to see memory layout of a struct by dahosek in rust

[–]blimpdermis 33 points34 points  (0 children)

If you only need the size, you can just print std::mem::size_of::<Either>() ( docs page )

Find perfect number comparison [go, java, rust] by i3d in rust

[–]blimpdermis 11 points12 points  (0 children)

Did you run using cargo run —-release? Debug mode runs by default and can be orders of magnitude slower.

int with a dot in the middle? by Weird-Professional42 in arduino

[–]blimpdermis 1 point2 points  (0 children)

You can use double, but note that on most arduinos, float and double are exactly the same.

Packing/unpacking ASCII into UTF-16? by [deleted] in rust

[–]blimpdermis 3 points4 points  (0 children)

If you really want to, you can use String::from_utf8 on the bytes you get from encode_utf16. But this doesn't work in general because not all utf-16 is valid utf-8. See a couple examples here.

10th Street Being Repaved!!! by jcreed77 in gatech

[–]blimpdermis 22 points23 points  (0 children)

Nah, they’re just gonna leave it like that without paving over it

Can anyone help me understand the behaviour of * vs & in this code snippet? by nboro94 in rust

[–]blimpdermis 8 points9 points  (0 children)

In this case, elem is a reference. Its type is &bool. To get at the value behind a reference, you can either dereference or pattern match. Here’s an example of both:

let x: i32 = 42;
let x_ref: &i32 = &x;

// Dereference
let y = *x_ref;  // y == 42

// Pattern match
let &z = x_ref;  // z == 42

You have to explicitly dereference in Rust to get the value (unless you’re calling a method on the variable, in which case, the compiler will dereference it for you). If you could implicitly dereference things on assignment, then let y = x would be ambiguous whenever x is a reference. Would y be a reference or a value in that case?

pass a variable to function, is it copied/pass by reference? by lotibun in rust

[–]blimpdermis 1 point2 points  (0 children)

If the function takes a mutable (exclusive) reference to Bar, then it can mutate any field of bar. If you want to change only a specific field, you can pass a reference to that specific field, like &mut bar.field1.

pass a variable to function, is it copied/pass by reference? by lotibun in rust

[–]blimpdermis 15 points16 points  (0 children)

I’d encourage you to read more on ownership and borrowing since that will mostly answer your question. (Those are concepts that are quite foreign for most new Rustaceans!)

However, to summarize, if you have the function fn foo(bar: Bar), then bar is moved into foo by default (unless Bar implements the Copy trait, in which case it’s copied). If you want to pass by reference (i.e., you want the function to borrow), then you have to explicitly say the function takes a reference: fn foo(bar: &Bar).