These are chat archives for rust-lang/rust

9th
May 2017
Michal 'vorner' Vaner
@vorner
May 09 2017 11:15
Hello. I tried the cargo-lichking tool (which checks licenses and their compatibilities) and it complained about my crate having a dependency with incompatible license. And I was wondering, should some kind of warning be issued on attempt to cargo publish or through crates.io somehow? Because it was kind of surprise for me (and I still have to figure what I'll do about it). Is this the right place to ask, or is there a better one?
Michael Thomas
@Michaelt293
May 09 2017 11:43
Sorry for asking a lot of basic questions here, is there anyway to do something x = undefined or x = ??? in Rust?
Serhii Plyhun
@snuk182
May 09 2017 11:49
@Michaelt293 there are no untyped or uninitialized variables in Rust (outside the unsafe blocks) at all
Michael Thomas
@Michaelt293
May 09 2017 11:50
okay, thanks :)
going to be a while till I get this to compile then while I fill in the blanks.
Ilya Bogdanov
@vitvakatu
May 09 2017 11:52
@Michaelt293 you can do it with unsafe code
see https://doc.rust-lang.org/std/mem/fn.uninitialized.html
Masaki Hara
@qnighy
May 09 2017 11:54
Possible alternatives for uninitialization are let x : T;, let x : Option<T> = None, let x : T = Default::default();depending on circumstances.
Michael Thomas
@Michaelt293
May 09 2017 11:56
thanks a lot
Sherzod Mutalov
@shmutalov
May 09 2017 12:02
@frankmcsherry
I've just replaced all nasty Into<String>'s to AsRef<str>, because of borrowing issues. Now it works 146% faster!!!
shmutalov/gitter-rs@b54f1a7
Frank McSherry
@frankmcsherry
May 09 2017 12:04
Hey sweet!
Jonas Platte
@jplatte
May 09 2017 12:28
@Michaelt293 Yeah you can actually declare a variable without initializing it, but only if the compiler can prove that it will be initialized before it is used.
E.g. this works
let x;

if some_condition {
    x = 2;
} else {
    x = 1;
}

foo(x);

Although you would probably rather write

let x = if some_condition { 2 } else { 1 };
foo(x);

in that case.

Martell Malone
@martell
May 09 2017 14:58

Hey tried to bootstrap rust in msys2 over the weekend with mingw-w64 for 1.17.0
Meant to ask this here before I revisit.

error[E0412]: type name `u128` is undefined or not in scope
   --> C:\msys64\home\Martell\rust-stuff\MINGW-packages\mingw-w64-rust\src\rustc-1.17.0-src\src\libcore\fmt\num.rs:263:25

Anyone got any idea why u128 is not a type?

Martell Malone
@martell
May 09 2017 15:04
no I am using stable
Martell Malone
@martell
May 09 2017 15:21
   Compiling libc v0.2.21
   Compiling getopts v0.2.14
   Compiling gcc v0.3.43
   Compiling rustc-serialize v0.3.22
   Compiling num_cpus v0.2.13
   Compiling filetime v0.1.10
   Compiling build_helper v0.1.0 (file:///C:/msys64/home/Martell/rust-stuff/MINGW-packages/mingw-w64-rust/src/rustc-1.17.0-src/src/build_helper)
   Compiling cmake v0.1.21
   Compiling toml v0.1.30
   Compiling bootstrap v0.0.0 (file:///C:/msys64/home/Martell/rust-stuff/MINGW-packages/mingw-w64-rust/src/rustc-1.17.0-src/src/bootstrap)
    Finished debug [unoptimized] target(s) in 13.3 secs
Building stage0 std artifacts (x86_64-pc-windows-gnu -> x86_64-pc-windows-gnu)
   Compiling unwind v0.0.0 (file:///C:/msys64/home/Martell/rust-stuff/MINGW-packages/mingw-w64-rust/src/rustc-1.17.0-src/src/libunwind)
   Compiling libc v0.2.21
   Compiling core v0.0.0 (file:///C:/msys64/home/Martell/rust-stuff/MINGW-packages/mingw-w64-rust/src/rustc-1.17.0-src/src/libcore)
   Compiling gcc v0.3.43
   Compiling libc v0.0.0 (file:///C:/msys64/home/Martell/rust-stuff/MINGW-packages/mingw-w64-rust/src/rustc-1.17.0-src/src/rustc/libc_shim)
   Compiling filetime v0.1.10
   Compiling build_helper v0.1.0 (file:///C:/msys64/home/Martell/rust-stuff/MINGW-packages/mingw-w64-rust/src/rustc-1.17.0-src/src/build_helper)
error[E0412]: type name `i128` is undefined or not in scope
  --> C:\msys64\home\Martell\rust-stuff\MINGW-packages\mingw-w64-rust\src\rustc-1.17.0-src\src\libcore\num/i128.rs:17:19
Serhii Plyhun
@snuk182
May 09 2017 15:22
afaik you physically cannot use feature gated stuff from nightly on stable
Martell Malone
@martell
May 09 2017 15:23
I am not trying to use nightly stuff, I am just trying to bootstrap a stable rust compiler
Martell Malone
@martell
May 09 2017 15:38
I created a PR and a note for other maintainer so hopefully it will get resolved soon.
Alexpux/MINGW-packages#2448
Zachary Golba
@zacharygolba
May 09 2017 16:02
@frankmcsherry I found an interesting feature in nightly that helps with what I was trying to do yesterday! I’m still not entirely sure what’s going on behind the scenes and what allocations are being made but it seems like it is efficient. Let me know what you think!
#![feature(conservative_impl_trait)]

fn print_three<'a>(x: &'a str) ->
impl Fn(&'a str) -> impl Fn(&'a str) -> () + 'a {
    move |y| move |z| println!("{}, {}, {}", x, y, z)
}

fn main() {
    print_three("1")("2")("3");
    // => 1, 2, 3
}
Frank McSherry
@frankmcsherry
May 09 2017 16:03
This will certainly be more efficient than your previous version.
Zachary Golba
@zacharygolba
May 09 2017 16:03
awesome!
Frank McSherry
@frankmcsherry
May 09 2017 16:04
The difference (perhaps you know already) is that it returns unboxed closures. You've also changed it to borrow the strings, which .. is different and could be better or worse (you can't transfer ownership or return it as easily).
Zachary Golba
@zacharygolba
May 09 2017 16:04
I was just about to ask what the difference was. Thanks for explaining!
Frank McSherry
@frankmcsherry
May 09 2017 16:05
Right, the main one is that impl indicates that you are returning a concrete type, but it is too difficult to actually name it (and for closures, you literally cannot name the type). Type inference can nonetheless propagate the concrete type, and it will move around as a stack-allocatable struct rather than a boxed thingy.
Zachary Golba
@zacharygolba
May 09 2017 16:06
Ah that makes sense. This is pretty cool stuff. Thanks again! :)
Frank McSherry
@frankmcsherry
May 09 2017 16:07
The &'a str arguments mean that it may be tricky to return this "up" to folks who live longer than the borrows you have for the strings. For example, if I hand you "Frank" and you want to put this closure together, you can only use it for the same lifetime as that borrow lasts, often the scope in which it is constructed.
Sherzod Mutalov
@shmutalov
May 09 2017 16:08
are there cargo developers here?
Frank McSherry
@frankmcsherry
May 09 2017 16:08
The previous version owned Strings which mean you could hand it around all over the place. Trade-offs there; this version is "lighter" if it is sufficiently usable, the previous version was more broadly useful but would sit on allocated data more. Fortunately, Rust wont let you get it wrong, and use the first version where you shouldn't.
Sathya Narrayanan
@sourcepirate
May 09 2017 16:32
fn fact(n: u32) -> u32{
    if n >= 2{
        n * fact(n-1)
    } else {
        1
    }
}

fn binomial(n: u32, p: f32, k:u32) -> f64{
   let pf = p::powi(k);
   let kf = p::powi(n-k);
   (fact(n) / (fact(n-k) * fact(k))) * pf * kf
}

fn main(){
    let n :u32 = 100;
    let k :u32 = 10;
    let p :f32 = 0.5;
    let result: f64 = binomial(n, p, k);
    println!("{:?}", result);
}

// let pf = p::powi(k);
//               ^^^^^^^ Use of undeclared type or module `p`
I was actually trying to execute the above program doing so encountered the following error. I am not sure why this is happening.
is it due to rust ownership ??
Denis Lisov
@tanriol
May 09 2017 16:34
p.powi(k)
Sathya Narrayanan
@sourcepirate
May 09 2017 16:35
Oops!
Sean Leffler
@sdleffler
May 09 2017 16:36
@sourcepirate be careful mixing all those number types together, also - you can only multiply, divide, add, subtract, etc. two of the same type. You're going to have to convert all those explicitly
Sathya Narrayanan
@sourcepirate
May 09 2017 16:36
forget that thanks
Sean Leffler
@sdleffler
May 09 2017 16:36
Rust will scream bloody murder if you don't :P
Sathya Narrayanan
@sourcepirate
May 09 2017 16:36
@sdleffler sure
Sathya Narrayanan
@sourcepirate
May 09 2017 17:10
how can i use range operation on a floating point numbers...
say something like 0.1..0.3
Ilya Bogdanov
@vitvakatu
May 09 2017 17:13
@sourcepirate you can use smth like
for i in 1..3 {
use_i(i * 0.1);
}
Gwen Lofman
@glfmn
May 09 2017 17:14
Is this less performant?
for i in (1..3).map(|x| x*0.1) {
    use_i(i)
}
Ilya Bogdanov
@vitvakatu
May 09 2017 17:15
@Lionex I almost sure it's the same
David McGillicuddy
@djmcgill
May 09 2017 18:10

I'm having some trouble using compiler_builtins_lib: https://doc.rust-lang.org/beta/unstable-book/library-features/compiler-builtins-lib.html
More specifically using the library cortex-m-rt: https://github.com/japaric/cortex-m-rt

The doc page I linked says:

When you link to this crate, make sure it only appears once in your crate dependency graph. Also, it doesn't matter where in the dependency graph you place the compiler_builtins crate.

In my crate I have in the cargo.toml:

[dependencies.compiler_builtins]
features = ["mem"]
git = "https://github.com/rust-lang-nursery/compiler-builtins"
stage = 1

[dependencies.cortex-m-rt]
version = "0.2.0"
features = ["default", "exceptions"]

But when I compile I get the error:

cortex-m-quickstart $ xargo rustc --target thumbv6m-none-eabi
warning: unused manifest key: dependencies.compiler_builtins.stage
   Compiling cortex-m-quickstart v0.1.1 (file:///Users/davidmcgillicuddy/private/cortex-m-quickstart)
   Compiling cortex-m-rt v0.2.1
   Compiling compiler_builtins v0.1.0 (https://github.com/rust-lang-nursery/compiler-builtins#280d19f1)
   Compiling cortex-m-semihosting v0.1.3
   Compiling r0 v0.2.1
   Compiling cortex-m v0.2.6
   Compiling vcell v0.1.0
   Compiling volatile-register v0.2.0
error[E0463]: can't find crate for `compiler_builtins`
   --> /Users/davidmcgillicuddy/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-rt-0.2.1/src/lib.rs:162:1
    |
162 | extern crate compiler_builtins;
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to previous error

error: Could not compile `cortex-m-rt`.
Build failed, waiting for other jobs to finish...
error: build failed
What am I doing wrong?