Giter Site home page Giter Site logo

rust-sscanf's Introduction

Sscanf in RUST using macros

Language System

Rust implementation of C function sscanf. It is not entirely equal, as it receives a string and a array of separators, instead of a formatted string with types.

Not using any extern crates, but unstable feature declarative macros 2.0.

Using macro_rules! works fine and will compile on stable.

Usage

let a :u8;   
let b :i32;   
let c :String;   
let d :f32;

let buffer = "02:98;abc,39.6";
let sep = [':',';',',',' '];

scan::sscanf!(buffer,sep,[u8,i32,String,f32],a,b,e,d);

println!("{} {} {} {}",a,b,e,d);

Result

2 98 abc 39.6

Notes

  • Using nightly-x86_64-unknown-linux-gnu.
  • Using Macro 2.0 instead of macro_rules! , as it seems to be getting deprecated, but works in both situations.
  • Using #![feature(decl_macro)]

Known Issue

So far the string variable has to be of the type String, since the macro cant receive a &str as argument, otherwise it would expand into invalid code. The problem with using String, is that is creates a copy of the parsed &str in the buffer. Using &str would be ideal performance wise. A better implementation has been discussed in this topic, adressing the problem with functions as well.

rust-sscanf's People

Contributors

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