Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In this case, "i" is a type suffix meaning "int". 5u would be an unsigned int; 5i32 would be a 32-bit int; 5f64 would be a double precision float, and so forth.


I understood that, but it's an unfortunate suffix.


I agree. Isn't it the default though? Could be implied when you choose to leave it out.


Yes it could be implied if left out. Due to type inference.

`5` would be signed or unsigned if left off. Depends on how you use it.

So you could try.

    use std::num::abs;

    let v = vec!['a', 'b', 'c', 'd'];
    let x = 3;
    let c = v[x];    // v.index (operator overload) constrains x to type uint.

    let y = abs(x);  // error: failed to find an implementation of trait core::num::Signed for uint.
                     // abs can't constain the type to an int because abs can take anything that          
                     // implements  `Signed` (eg BigInt)
                    
If you wanted to make sure that x was signed.

    let v = vec!['a', 'b', 'c', 'd'];
    let x = 3i;
    let y = abs(x);  // Ok as x is signed.
    let c = v[x];    // error: mismatched types: expected `uint` but found `int`
If you really wanted to index to try an index with an int you can use a checked cast.

    let casted = x.to_uint(); // Returns None if x is negative or Some(x) if it's postive.

Also a generic int doesn't have to be int or uint, it can be any integer value: signed: i8, i16, i32, i64 or int. Unsigned: u8, u16, u32, u64 or uint.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: