These are chat archives for rust-lang/rust

23rd
Mar 2017
Vladimir
@Crazy-Owl
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
@Xaeroxe
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: https://doc.rust-lang.org/rand/rand/fn.thread_rng.html
John Brandes
@John0x
Mar 23 2017 20:20
hey :)
how can i print the address of a var?
(memory address)
Sergey Noskov
@Albibek
Mar 23 2017 20:24
that's not what you usually do in Rust, maybe using GDB will be better variant?
Jacob Kiesel
@Xaeroxe
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
@John0x
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
@Xaeroxe
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
@John0x
Mar 23 2017 20:37
ah, alright, thank :)
you
Thomas Jay Rush
@tjayrush
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
@retep998
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
@tjayrush
Mar 23 2017 22:38
How about void pointers to data?