stevedonovan/gentle-intro

Example does not compile

Opened this issue · 5 comments

First example at http://stevedonovan.github.io/rust-gentle-intro/1-basics.html#learning-where-to-find-the-ropes

 #![allow(unused_variables)]
 
fn main() {
let pi = 3.1416;
let x = pi/2.0;
let cosine = x.cos();

}

output when run in browser is

   Compiling playground v0.0.1 (file:///playground)
error[E0599]: no method named `cos` found for type `{float}` in the current scope
 --> src/main.rs:6:16
  |
6 | let cosine = x.cos();
  |                ^^^

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

I know this is probably something trivial. But I'm a total n00b, and that's why I'm reading this tutorial!

Yes, it really was impossible to compile that snippet! Rust needs to know whether pi is a single or a double float (f32 or f64) so putting an explicit type let pi: f64 = ... sorts it out. It's because the standard library defines cos for both kinds of floating point number.

Fixed with the latest update!

Interesting. Looks like that's an opportunity for a better compiler error message.

error[E0599]: no method named cosfound for type{float} in the current scope

Didn't to me at all suggest what the cause of the problem was. Thanks for the help!

... and the very awesome rust intro. I really appreciate the references to other programming languages. Besides references to c/c++, it's something I haven't yet seen in other rust books and tutorials.

I think we all come from somewhere, and usually it's a garbage-collected, managed language. That definitely makes life simpler. With Rust certain new distinctions have to be made, but really it's mostly about references and making sure they live just long enough.

With the new update, I included a new chapter on "Pain Points". I think this is probably the place where detailed language comparisons should go!

Actually, other tutorials and sources are at least decent about describing borrows, and how they compare to c/c++. That's actually not what I was referencing. What I appreciate are all the non-memory-management-related language comparisons and references. I know C, but I'm most proficient in java and python. For example... identifying that traits are very close to java interfaces. It was like a fast-forward in my understanding of how they work. I eventually would have recognized it on my own, and would have used that exact same connection in my own mental model. The cool part was having somebody actually telling me it's so, rather than me having to make that connection on my own, and then question and prove to myself whether it's valid or not.