This file contains hidden or 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
fn save_status(text: &str) -> Result<i64, &'static str> { | |
if text.len() > 200 { | |
return Err("status is too long, must be under 200 bytes"); | |
} | |
let id = save_to_database(text)?; | |
// log id to server logs |
This file contains hidden or 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
fn main() { | |
let z = 3; | |
let mut p = &z; | |
{ | |
let x = 5; | |
p = &x; | |
p = &z; | |
} | |
This file contains hidden or 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
fn main() { | |
let z = 3; | |
let mut p = &z; | |
{ | |
let x = 5; | |
p = &x; | |
} | |
} |
This file contains hidden or 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
fn main() { | |
let z = 3; | |
let r; | |
{ | |
let x = 5; | |
let mut p = &x; | |
p = &z; | |
r = p; | |
} |
This file contains hidden or 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
fn main() { | |
let list = vec![0, 1, 2, 3, 4, 5]; | |
let index = 3; | |
if true { | |
println!("the magic number is {}", list[index]); | |
} else { | |
let other_index = 5; | |
println!("the index is {}", other_index); // so you wouldn't get "unused variable other_index" | |
println!("the magic number is {}", list[index]); |
This file contains hidden or 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
fn main() { | |
let mut a_vec = vec![1, 2, 3]; | |
let mut a_slice = &mut a_vec[..]; | |
let ref mut an_item = a_slice[0]; | |
let mut foo = &mut an_item; | |
} |
This file contains hidden or 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
// The error is that there's an unused generic G declared... | |
fn foo<G>() -> String { | |
String::from("hi") | |
} | |
fn main() { | |
println!("{}", foo()); | |
// ^ but here is where the error message points to: | |
// error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282] |
This file contains hidden or 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
// Make these tests pass by filling in conditionals where the blanks currently are! | |
// Scroll down for hints :) | |
pub fn fizz_buzz(i: i32) -> String { | |
__________ { | |
"FizzBuzz".to_string() | |
} ___________ { | |
"Fizz".to_string() | |
} ___________ { | |
"Buzz".to_string() |
This file contains hidden or 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
fn main() { | |
// I don't want this to have to be mut. | |
let v = vec![1, 2, 3]; | |
// Not this either. | |
let w = vec![3, 4, 5]; | |
// I want a newly allocated vec that is v + w. | |
let x = vec_concat(&v, &w); | |
println!("{:?}", x); |
This file contains hidden or 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
$ irb | |
1.9.3p327 :001 > logfile = File.new('some_file.txt', 'w') | |
=> #<File:some_file.txt> | |
1.9.3p327 :002 > logfile.write("my content") | |
=> 10 | |
1.9.3p327 :003 > quit | |
$ cat some_file.txt | |
my content | |
$ irb | |
1.9.3p327 :001 > logfile = File.new('some_file.txt', 'w') |
NewerOlder