Last active
May 29, 2019 15:59
-
-
Save rokob/352cef8b0c843cb52282d361ad5cd5c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate libc; | |
use libc::size_t; | |
#[link(name = "snappy")] | |
extern { | |
fn snappy_max_compressed_length(source_length: size_t) -> size_t; | |
} | |
fn main() { | |
let x = unsafe { snappy_max_compressed_length(100) }; | |
println!("max compressed length of a 100 byte buffer: {}", x); | |
} | |
#[no_mangle] | |
pub extern fn hello_rust() -> *const u8 { | |
"Hello, world!\0".as_ptr() | |
} | |
#[no_mangle] | |
#[allow(unused_variables)] | |
pub extern "C" fn Agent_OnLoad( | |
vm: *mut JavaVM, | |
options: *mut c_char, | |
reserved: *mut c_void, | |
) -> jint { | |
pretty_env_logger::init_custom_env("ROLLBAR_LOG"); | |
info!("Agent load begin"); | |
if let Err(e) = onload(vm) { | |
return e; | |
} | |
if !initialize_configuration() { | |
return jvmtiError_JVMTI_ERROR_INTERNAL as i32; | |
} | |
info!("Agent load complete success"); | |
INIT_SUCCESS.store(true, Ordering::Relaxed); | |
0 | |
} | |
pub trait Summary { | |
fn summarize_author(&self) -> String; | |
fn summarize(&self) -> String { | |
format!("(Read more from {}...)", self.summarize_author()) | |
} | |
} | |
pub struct Tweet { | |
pub username: String, | |
pub content: String, | |
pub reply: bool, | |
pub retweet: bool, | |
} | |
impl Summary for Tweet { | |
fn summarize_author(&self) -> String { | |
format!("@{}", self.username) | |
} | |
} | |
let tweet = Tweet { | |
username: String::from("horse_ebooks"), | |
content: String::from("of course, as you probably already know, people"), | |
reply: false, | |
retweet: false, | |
}; | |
println!("1 new tweet: {}", tweet.summarize()); | |
1 new tweet: (Read more from @horse_ebooks...) | |
pub enum Option<T> { | |
None, | |
Some(T), | |
} | |
let optional = None; | |
check_optional(optional); | |
let optional = Some(Box::new(9000)); | |
check_optional(optional); | |
fn check_optional(optional: Option<Box<i32>>) { | |
match optional { | |
Some(ref p) => println!("has value {}", p), | |
None => println!("has no value"), | |
} | |
} | |
let msg = Some("howdy"); | |
if let Some(ref m) = msg { | |
println!("{}", *m); | |
} | |
let unwrapped_msg = msg.unwrap_or("default message"); | |
enum Result<T, E> { | |
Ok(T), | |
Err(E), | |
} | |
fn write_info(info: &Info) -> io::Result<()> { | |
let mut file = match File::create("my_best_friends.txt") { | |
Err(e) => return Err(e), | |
Ok(f) => f, | |
}; | |
if let Err(e) = file.write_all(format!("name: {}\n", info.name).as_bytes()) { | |
return Err(e) | |
} | |
Ok(()) | |
} | |
fn write_info(info: &Info) -> io::Result<()> { | |
let mut file = File::create("my_best_friends.txt")?; | |
file.write_all(format!("name: {}\n", info.name).as_bytes())?; | |
Ok(()) | |
} | |
8-bit i8 u8 | |
16-bit i16 u16 | |
32-bit i32 u32 | |
64-bit i64 u64 | |
128-bit i128 u128 | |
arch isize usize | |
f32 | |
f64 | |
bool | |
char | |
let tuple: (i32, f64, bool) = (-1, 3.14, true); | |
let array: [i32; 5] = [1, 2, 3, 4, 5]; | |
enum Message { | |
Quit, | |
Move { x: i32, y: i32 }, | |
Write(String), | |
ChangeColor(i32, i32, i32), | |
} | |
fn handle_message(msg: Message) { | |
match msg { | |
Message::Quit => panic!("bye"), | |
Message::Move { x, y } => do_move(x, y), | |
Message::Write(_) => println!("hello"), | |
Message::ChangeColor(r, _, _) => make_red(r), | |
} | |
} | |
fn handle_message(msg: Message) { | |
match msg { | |
Message::Quit => panic!("bye"), | |
Message::Move { x, y } => do_move(x, y), | |
Message::Write(_) => println!("hello"), | |
_ => unimplemented!(), | |
} | |
} | |
fn handle_message(msg: Message) { | |
match msg { | |
Message::Quit => panic!("bye"), | |
Message::Move { x, y } => do_move(x, y), | |
Message::Write(_) => println!("hello"), | |
} | |
} | |
struct Rectangle { | |
width: u32, | |
height: u32, | |
} | |
impl Rectangle { | |
fn area(&self) -> u32 { | |
self.width * self.height | |
} | |
fn square(size: u32) -> Rectangle { | |
Rectangle { width: size, height: size } | |
} | |
fn can_hold(&self, other: &Rectangle) -> bool { | |
self.width > other.width && self.height > other.height | |
} | |
} | |
fn main() { | |
let rect1 = Rectangle { width: 30, height: 50 }; | |
let rect2 = Rectangle::square(60); | |
println!( | |
"The area of the rectangle is {} square pixels.", | |
rect1.area() | |
); | |
println!("Can it hold? {}", rect2.can_hold(rect1)); | |
} | |
fn main() { | |
let greeting = greet(true); | |
println!("{}", greeting); | |
} | |
fn greet(v: bool) -> String { | |
if v { | |
"Hello, world!".to_string() | |
} else { | |
"Hello, Rust!".to_string() | |
} | |
} | |
#[test] | |
fn test_greet() { | |
assert_eq!("Hello, world!", greet(false)) | |
} | |
let x = 5; | |
let y = x; | |
println!("{}", x); | |
let s1 = String::from("hello"); | |
let s2 = s1; | |
println!("{}, world!", s1); | |
fn main() { | |
let s1 = gives_ownership(); | |
let s2 = String::from("hello"); | |
let s3 = String::from("bork"); | |
takes_ownership(s3); | |
let s4 = takes_and_gives_back(s2); | |
} | |
fn gives_ownership() -> String { | |
let some_string = String::from("hello"); | |
some_string | |
} | |
fn takes_and_gives_back(a_string: String) -> String { | |
a_string | |
} | |
fn takes_ownership(some_string: String) { | |
println!("{}", some_string); | |
} | |
fn main() { | |
let s1 = String::from("hello"); | |
let len = calculate_length(&s1); | |
println!("The length of '{}' is {}.", s1, len); | |
} | |
fn calculate_length(s: &String) -> usize { | |
s.len() | |
} | |
fn main() { | |
let s = String::from("hello"); | |
change(&s); | |
} | |
fn change(some_string: &String) { | |
some_string.push_str(", world"); | |
} | |
fn main() { | |
let mut s = String::from("hello"); | |
change(&mut s); | |
} | |
fn change(some_string: &mut String) { | |
some_string.push_str(", world"); | |
} | |
let mut s = String::from("hello"); | |
let r1 = &mut s; | |
let r2 = &mut s; | |
println!("{}, {}", r1, r2); | |
let mut s = String::from("hello"); | |
let r1 = &s; | |
let r2 = &s; | |
let r3 = &mut s; | |
println!("{}, {}, and {}", r1, r2, r3); | |
let mut s = String::from("hello"); | |
let r1 = &s; | |
let r2 = &s; | |
println!("{} and {}", r1, r2); | |
let r3 = &mut s; | |
println!("{}", r3); | |
fn main() { | |
let reference_to_nothing = dangle(); | |
} | |
fn dangle() -> &String { | |
let s = String::from("hello"); | |
&s | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment