These are chat archives for rust-lang/rust

Mar 2017
Mar 23 2017 05:42
Oh, I've just meant that I can't specify any boundaries for generated values of specific types
Jacob Kiesel
Mar 23 2017 18:15
@Crazy-Owl use the ThreadRng type in the Rand crate to get random values in specific ranges
You can make a ThreadRng with this function:
John Brandes
Mar 23 2017 20:20
hey :)
how can i print the address of a var?
(memory address)
Sergey Noskov
Mar 23 2017 20:24
that's not what you usually do in Rust, maybe using GDB will be better variant?
Jacob Kiesel
Mar 23 2017 20:26

@John0x Not sure why you'd want to do that, but I just threw this together:

fn print_address<T>(pointer: &T) {
    println!("Reference address: {:?}", (pointer as *const T))

Whatever you're trying to do there's probably a better way to do it but if there isn't that's how you get the address

John Brandes
Mar 23 2017 20:30
@Xaeroxe so the "as *const" is casting it to it's address, whereas "println!("add: {:p}", &4)" would print the address of a pointer to the value?
I'm just trying to understand :)
Coming from c++
Jacob Kiesel
Mar 23 2017 20:33
Not quite, as *const casts from a Rust reference to a raw pointer, we use the {:?} inside of the println string to get the debug print of that raw pointer, which happens to display the actual memory address used by the pointer. Rust references are not C style pointers and if you asked for their debug print it would instead use the debug print of what the reference is referencing.
Rust's "raw pointer" is a C style pointer
Their use is discouraged in Rust unless you're integrating with C code though
John Brandes
Mar 23 2017 20:37
ah, alright, thank :)
Thomas Jay Rush
Mar 23 2017 22:15
In some of my C libraries, I use function pointers for things like forEveryWhatever. And then the function is called on every whatever (file, folder, etc.). Is there any possibility that these functions would be callable with Rust functions assuming I can integrate c libraries with rust. My guess is no, but I thought I'd ask.
Peter Atashian
Mar 23 2017 22:28
@tjayrush I don't see why you wouldn't be able to do that.
Rust can call C functions and it can also define its own C functions for other things to call
Thomas Jay Rush
Mar 23 2017 22:38
How about void pointers to data?