Like Tree31Likes

Let's talk about Rust!

This is a discussion on Let's talk about Rust! within the General Discussions forums, part of the Community Boards category; Apparently not. You can write: Code: let x; x = 1; print!("{}", x); the compiler knows that it wasn't used ...

  1. #166
    Registered User
    Join Date
    Jun 2014
    Posts
    13
    Apparently not. You can write:

    Code:
    let x;
    x = 1;
    print!("{}", x);
    the compiler knows that it wasn't used before it was initialized and it will compile and print 1
    if you write

    Code:
    let x;
    print!("{}", x);
    x = 1;
    the compiler will barf at you

    Code:
    const.rs:3:18: 3:19 error: use of possibly uninitialized variable: `x`
    const.rs:3     print!("{}", x);
                                ^
    note: in expansion of format_args!
    <std macros>:2:23: 2:75 note: expansion site
    <std macros>:1:1: 3:2 note: in expansion of print!
    const.rs:3:5: 3:21 note: expansion site
    error: aborting due to previous error

  2. #167
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    Quote Originally Posted by MutantJohn View Post
    Whoa, I'm tripping out. Either I don't know enough about const-ness or you're mutating an immutable variable...

    Or can read-only variables be initialized with a value once and only once? Or does the initialization have to happen at declaration? I always thought it was at declaration.
    Apparently the compiler is smart enough to realize that the variable is going to get initialized once and only once and hence does not barn.
    The same thing can be done using constexpr in C++14 I believe (I didn't try).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #168
    Registered User
    Join Date
    Jun 2014
    Posts
    13
    this can be done because Rust has a loop() so it knows you can only escape via break

    Java does the same thing in case of for(;;) and while(true) but not in other cases like while(1 == 1) so it's a little bit embarassing that while(true) and while(1 == 1) don't work the same way

Page 12 of 12 FirstFirst ... 23456789101112
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do I talk too much?
    By GoodStuff in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-20-2003, 11:45 PM
  2. Who wants to talk on AIM?
    By Death Wish in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-05-2002, 07:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21