Like I Don't Know About Rust (HELP)

Hello, I am new to learning pas and have experience in C#, C/C++, Assembly, Java, Python, Javascript, Ruby and many more. To be honest, I had no problem learning Rust. It's very easy and simple right now, I'm in the 7th chapter of the book, but for example, I haven't learned the subjects I learned,
I feel like I'm doing something wrong.

but I can easily understand what a rust code does.

Has this happened to you before?

I will read the rust nomicon book after the rust book
Can you suggest sample projects to improve myself very well on rust?

I don't understand your English very well (and I'm not a native English speaker myself), so let me ask a few questions:

What do you mean with "pas"? Is this a typo?

Do you mean that Chapter 7 on packages, crates, and modules is more difficult? Or do you mean that you have the feeling to have forgotten or misunderstood things that you thought you had correctly learned before?

1 Like

I'm interpreting this as:

  1. you have read a lot of Rust, and things make sense

  2. you have written very little Rust, and thus are not confident in your understanding

If that is the case, the solution is to write lots of Rust.

I am a big fan of typing out the examples in Introduction - Rust By Example and fighting the compiler errors. It builds muscle memory and confidence for battling compiler errors.

1 Like

Turkish Language = pas
English Language = rust

1 Like

Thanks for help

Yeah, I know the things I've learned before but sometimes I feel like I don't know

It's always a good idea to practice anything that you are trying to learn. Otherwise it willl hardly stick.

Another suggestion is to be selective on your next learning step(s). It's easier if you ask yourself a question with reasonable boundaries such as "how does Rust's borrow checker work?" instead of very nebulous ones such as "how do I learn Rust syntax?".

1 Like

I gave up learning Rust three times because I felt like it was too confusing. When I tried it for the fourth time, I managed to understand it finally.

I had a lot of situations where I thought I had understood everything, but then I ran into difficult problems.

So be sure that you're not the only person who experiences these difficulties. Rust is hard to learn (in my opinion), but it's a really beautiful programming language. My recommendation is to keep trying and to not be frustrated if something doesn't work. It's normal to run into problems, and it takes time to learn Rust.

It can help to ask about specific problems you encounter in this forum.

Another possibility is to first gain some practice on basic concepts. Using owned values and .clone() can sometimes make things more easy compared to using references in a lot of places. This way, you can avoid some complex lifetime issues (at the expense of a slower program).

Which topics are you having difficulties with?

1 Like

thank you your answer helped me a lot so far I have understood many topics very well but 5u32 i don't understand what that means

The type of 5 isn't fully specified. It's an integer, but it could be an i32 or an u8, for example.

Appending i32 or u8 as a suffix to 5 will tell the compiler that 5 is of type i32 or u8. The corresponding section in the reference is here.

Also see this example:

fn main() {
    let a = 5i32; // the same as: let a: i32 = 5;
    let b = 5u8; // the same as let b: u8 = 5;
    // now both `a` and `b` are integers,
    // but `a` is 32 bit signed and `b` is 8 bit unsigned
    
    println!("{a} {b}");
    
    // The following fails because `a` and `b` are different types.
    // Try to uncomment:
    //assert_eq!(a, b);
    
    // But this works:
    assert_eq!(a, 5);
    assert_eq!(b, 5);

    // Also this:
    assert_eq!(a, 5i32);
    assert_eq!(b, 5u8);
    
    // But this fails:
    //assert_eq!(a, 5u8);
    //assert_eq!(b, 5i32);
}

(Playground)

Output:

5 5

actually it looks like this

let a: i32 = 5;
let b: u8 = 5;

let c = 123.0f64;
let c: f64 = 123.0;

(Output)
5, 5
123, 123

Yes, the following two are identical:

let a = 5i32;
let a: i32 = 5;

The advantage of 5i32 is that you can use it in any place (also when there is no let involved):

use num_traits::PrimInt;

// This function adds three numbers of any primitive integer type:
fn add_three_numbers<I>(a: I, b: I, c: I) -> I
where
    I: PrimInt,
{
    a + b + c
}

fn main() {
    println!("{}", add_three_numbers(10, 5, 6)); // compiler will use i32 here because it cannot infer any other type
    println!("{}", add_three_numbers(10u8, 5u8, 6u8)); // working with bytes
    //println!("{}", add_three_numbers(255u8, 1u8, 0u8)); // overflow!
    //println!("{}", add_three_numbers(2147483647, 1, 0)); // also overflowing!
    println!("{}", add_three_numbers(2147483647i64, 1i64, 0i64)); // we can avoid the overflow by explicitly requesting `i64`
}

(Playground)

Output:

21
21
2147483648

Wow, I love this very eye-catching rust

Maybe you know the L and LL suffixes for integers in C?

For example the following code gives me a warning:

int main() {
    long a = 2147483647 + 1;
    return 0;
}
test.c:2:25: warning: overflow in expression; result is -2147483648 with type 'int' [-Winteger-overflow]
    long a = 2147483647 + 1;

I can fix it (on my platform) with L:

int main() {
    long a = 2147483647L + 1L;
    return 0;
}

So 5i64 in Rust is similar to writing 5L in C. The difference is that in Rust you specify the exact bit length, while in C a "long int" varies depending on the platform.

2 Likes