How do I understand the logic of this syntax?

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.

I came to this link but I see nothing like the above syntax. Where does it say I need to unwrap etc? How do I even implement the features listed in the link?

The documentation on doesn't tell me anything about how to actually write the code in my program. Any tips on how to get better at this?

You read it like this:

  • Execute the window function from the web_sys module
  • Unwrap the returned object
  • Execute the document method
  • Unwrap the returned object

If you don't understand what .unwrap does, then I suggest you to read about the Result and Option types in Rust.


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();?

Yes, you can

use web_sys::Document;
is something like
path c:\myfolder\; c:\backups\programs\;

1 Like
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


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. 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 {
1 Like

Can always count on you haha.
Mostly I'm thinking aloud and it helps getting external explenation, so thanks :smile:

Makes more sense now, ty

1 Like

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 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!

How to understand...?

rustup doc

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?

1 Like

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.)

1 Like

Thanks for the tip, that'll help a lot I'm sure

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.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.