Need to return only if matches

need to return token only if path starts else do nothing

pub fn build(self, stream: &TcpStream) {
        let client_id = self.client_id;
        let scope = &self.scope;
        let state = self.state;
        let login = self.login;
        let redirect = self.redirect;

        let path = buffer_to_path(self.buffer);

        if path.starts_with("/login") {
            Self::oauth_screen(stream, client_id, scope, &state);
        }
        if path.starts_with("/callback?code=") {
            let code = github_authorization_code(&path);

            let authorization = github_access_token(
                code.to_string(),
                client_id,
                self.client_secret,
                redirect,
                stream,
            )
            .unwrap();

            let token = github_decode(&authorization);
            println!("{:?}", token);
        }
        // println!("{}", "ertyuiop");
    }```

Returning some type conditionally is usually done by wrapping it in an Option.

but need only value no none ?

Well, what do you want to return when your path doesn't start with your designated prefix?

return nothing if dont match
but return token only if matches path

Right. You can't exactly return nothing from a function that returns, say, a String. That would violate the function's signature. Even in OOP languages without null-safety an implicit null-pointer would be returned in cases where you return "nothing". Rust makes control flow more explicit, encoding the fact that a function might return "nothing" in its return type by returning Option<String> instead of String, for example, where Option::None indicates to the caller that "nothing" was returned.

1 Like

if there any other way to return value only if path matches else nothing

You can loop forever, or panic, or otherwise terminate the program.

but panic and terminating cause shutdown the server ?
is there any other way to handle to shift the token to another variable then use in main ?

Pass the variable you want to store the token in as mutable reference to the function and assign the token to it.

okie