Skip to content

Instantly share code, notes, and snippets.

Created July 19, 2017 06:00
Show Gist options
  • Save anonymous/a482ff0f5d17e92526200253b4198147 to your computer and use it in GitHub Desktop.
Save anonymous/a482ff0f5d17e92526200253b4198147 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
extern crate num;
use num::BigInt;
use num::bigint::ToBigInt;
pub struct GString(pub String);
pub struct Gstr<'a>(pub &'a str);
impl ToBigInt for GString {
fn to_bigint(&self) -> Option<BigInt> {
Some(BigInt::from_signed_bytes_le(self.0.as_bytes()))
}
}
impl<'a> ToBigInt for Gstr<'a> {
fn to_bigint(&self) -> Option<BigInt> {
GString(self.0.to_owned()).to_bigint()
}
}
impl GString {
pub fn from_bigint(src: &BigInt) -> String {
String::from_utf8(src.to_signed_bytes_le()).unwrap()
}
}
fn main(){
use num::Num;
use num::bigint::ToBigInt;
use {GString, Gstr};
let teststr = "那是一年春天";
let bigint = Gstr(teststr).to_bigint().unwrap();
let bigstr = bigint.to_str_radix(10);
let bigint2 = BigInt::from_str_radix(bigstr.as_str(), 10).unwrap();
let gstring = GString::from_bigint(&bigint2);
assert_eq!(teststr, gstring);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment