Giter VIP home page Giter VIP logo

kotlike's Introduction

Kotlike

A Rust macro to modify the question mark operator's behavior just like Kotlin's

With it, you can easily pick out the value wrapped in numberless Option<T> and Result<T,Err>.

Furthermore, it won't break down your control flow. You can continue your work even you got a None or Err (All the unexpected value will turn to None)

It means:

fn do_something() {
    let value: Option<TypeYourWant> = wrapped_value?.something_return_option()?.something_return_result()?.value;
}

would works fine! Just as Kotlin's style. And you don't need to worry about what Err it would throw!

How it Works

Usage

#[kotlike]
fn main() {
    let a = "Hello".to_string();
    
    let c = File::create("test.txt")?.write_all(a.as_bytes())?.clone();
    
    let mut b: String = String::new();
    
    let len = File::open("test.txt")?.read_to_string(&mut b)?.clone();
    
    println!("Hello, {:?}({:?})!", b,len);
}

Expand the macro would look like:

fn main() {
  let a = "Hello".to_string();
    
  let c: Option<()> = File::create("test.txt")
      .map_or(None, |mut v| {
          v.write_all(a.as_bytes())
              .map_or(None, |mut v| Some(v.clone()))
      });
    
  let mut b: String = String::new();
    
  let len: Option<usize> = File::open("test.txt")
      .map_or(None, |mut v|{
          v.read_to_string(&mut b)
              .map_or(None, |mut v| Some(v.clone()))
      });
    println!("Hello, {:?}({:?})!", b,len);
}

Above example is just showing how it works. Don't focus too much on what stupid code does.

LICENSE

Apache LICENSE 2.0

kotlike's People

Contributors

oxeu avatar

Stargazers

 avatar PBK Bin avatar YiNN avatar Hamster avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.