let document = web_sys::window().unwrap().document().unwrap();
I'm having a hard time reading and understanding documentation. I have no idea how to implement what I'm reading and I have no clue how to better this either right now.
Haven't found it in the book. But I can see that it's an option yes.
But what does use web_sys::Document; do than?
Cant I use that instead of unwrap it like so: let document = web_sys::window().unwrap().document().unwrap();?
let document = web_sys::window().unwrap().document().unwrap()
This part
let document = web_sys::window()
will put a thing in document. This thing is called an Option and the Option can either hold something, or have nothing. If the Option has something inside then the
unwrap()
will give the thing inside the Option. If the Option has nothing it is called None and if you unwrap an Option with None, the program "panics"
So ...
let document = web_sys::window().unwrap()
will put something in document if sys::window() returned an Option that was not None.
Then this thing has a function attached called document() and that function also returns an Option. If the second Option also has something inside, you get it. If the second Option is None, the program crashes.
Also...you could write this and not use the "." periods.
( not good rust style)
(written like simple c so you can "see" what happens)
fn main() {
let document = {
let window = web_sys::window();
if (window == None) {
panic!("nothing in window");
} else {
let document = window.document();
if (document == None) {
panic!("nothing in document");
} else {
document
}
}
};
}
The documentation of a library is usually a reference. It tells you what functionality the library exposes, and perhaps how the author intended it should be used. It is generally expected that you are familiar with the basics of the programming language itself – it's not the duty or scope of any particular library to teach you "how to write the code".
That purpose is fulfilled by the documentation and teaching material of the language proper; for Rust, your best bet is to start at https://doc.rust-lang.org and also search for "Rust by Example".
Yea makes sense. It's just hard this way to get started since everything is quite scattered. But luckily it seems doable with Rust since the language itself has some good documentations. I will look more into the examples as well!
Open up the book, the reference, rust-by-example, standard library in tabs.
The documentation is searchable Type "s" in your browser and enter a word you do not understand.
Repeat for each tab.
The documentation also links to itself. So when you read along you can click and go deeper.
The deeper links will send you places like the nomicon. or ?intrinsics? or back up to the top.
The references are recursive and have reference cycles that you can get caught in if you are not careful. Endlessly looping and never getting to the explanation you need.
Do you like fractals?
The best way to learn what some piece of unfamiliar code does is to load it up in an editor or IDE that provides two useful features:
Jump to definition/view documentation, so you can just click or hover over an unfamiliar function call and hopefully get some idea of what it does and whether it’s a part of Rust’s standard library or some third-party crate.
Inlay type hints, particularly valuable when trying to understand chains of method calls like your example. These annotate each method call with its return type, so you can see at a glance that, eg. window() returns an Option which is a Rust standard library type representing a possibly missing value. (Soon you’ll begin to recognize certain patterns such as any unwrap() call almost always meaning either Option::unwrap or Result::unwrap.)
Not particularly haha. I've been looping in the past but I just ended up in the same place.
The amount of tabs I have open is giving me a headache. I do like to keep going in, but it mostly prevents me from using the information practically as well.