The document appears to be a series of tweets by @brian_pearce discussing learning Rust and its differences from other languages like Python and Ruby. Some key points discussed include Rust's use of Result and Option types to handle errors and absent values instead of exceptions, and how Rust uses these types and match expressions to make error handling more explicit compared to try/catch blocks in other languages. Testing in Rust is also discussed with examples shown of setting up tests with a shared temp directory cleaned up after each test.
9. @brian_pearce
fn read_file() {
let mut data = String::new();
let mut f = File::open("my_file.txt").unwrap();
f.read_to_string(&mut data).unwrap();
for line in data.lines() {
process(line);
}
}
fn process(line: &str) {
if is_a_pun(line) {
tell_a_friend();
}
}
Rust
10. @brian_pearce
def read_file(argv)
with open('my_file.txt') as file:
for line in file: process(line)
fn read_file() {
let mut data = String::new();
let mut f = File::open("my_file.txt").unwrap();
f.read_to_string(&mut data).unwrap();
for line in data.lines() {
process(line);
}
}
Python
Rust
11. @brian_pearce
def read_file(argv)
with open('my_file.txt') as file:
for line in file: process(line)
fn read_file() {
let mut data = String::new();
let mut f = File::open("my_file.txt").unwrap();
f.read_to_string(&mut data).unwrap();
for line in data.lines() {
process(line);
}
}
Python
Rust
33. @brian_pearce
puts "Enter a number"
num = gets.chomp.to_i
if num.is_a?(Numeric)
puts "Great number choice: #{num}"
else
puts "That wasn't a number"
end
Ruby
34. @brian_pearce
Ruby
puts "Enter a number"
num = gets.chomp.to_i
if num.is_a?(Numeric)
puts "Great number choice: #{num}"
else
puts "That wasn't a number"
end
35. @brian_pearce
Rust
fn main() {
println!("Enter a number");
let mut input = String::new();
io::stdin().
read_line(&mut num).unwrap();
let int = num.to_digit(10).unwrap();
println!("Great number choice: {}", int);
}
36. @brian_pearce
fn main() {
println!("Enter a number");
let mut input = String::new();
io::stdin().
read_line(&mut num).unwrap();
let maybe = num.to_digit(10);
if maybe.is_some() {
let i = maybe.unwrap();
println!("Great number choice: {}", i);
} else if maybe.is_none() {
println!("That wasn't a number");
};
}
Rust
37. @brian_pearce
fn main() {
println!("Enter a number");
let mut input = String::new();
io::stdin().
read_line(&mut num).unwrap();
let maybe = num.to_digit(10);
if maybe.is_some() {
let i = maybe.unwrap();
println!("Great number choice: {}", i);
} else if maybe.is_none() {
println!("That wasn't a number");
};
}
Rust
38. @brian_pearce
fn main() {
println!("Enter a number");
let mut input = String::new();
io::stdin().
read_line(&mut num).unwrap();
let maybe = num.to_digit(10);
match maybe {
Some(n) => {
println!("Great number choice: {}", n);
},
None => {
println!("That wasn't a number");
}
};
Rust
39. @brian_pearce
thread '<unnamed>' panicked at 'cannot access a TLS value during
../src/libcore/option.rs:330
fatal runtime error: Could not unwind stack, error = 5
thread'<unnamed>'panickedat'cannotaccessaTLSva
fatalruntimeerror:Couldnotunwindstack,error=5
Error Handling
46. @brian_pearce
Rust
fn read_file() {
let mut data = String::new();
let mut f = File::open("my_file.txt").unwrap()
f.read_to_string(&mut data).unwrap();
for line in data.lines() {
process(line);
};
}
47. @brian_pearce
Rust
fn read_file() {
let mut data = String::new();
let mut f = File::open("my_file.txt").unwrap()
f.read_to_string(&mut data).unwrap();
for line in data.lines() {
process(line);
};
}
48. @brian_pearce
Rust
fn read_file() {
let mut data = String::new();
let mut f = File::open("my_file.txt").unwrap()
f.read_to_string(&mut data).unwrap();
for line in data.lines() {
process(line);
};
}
49. @brian_pearce
Rust
fn read_file() -> Result<String, String> {
let mut data = String::new();
let mut f = try!(File::open("my_file.txt"));
f.read_to_string(&mut data).unwrap();
for line in data.lines() {
process(line);
};
}
50. @brian_pearce
Rust
fn read_file() -> Result<String, String> {
let mut data = String::new();
let mut f = try!(
File::open("my_file.txt").map_err(|e| e )
);
f.read_to_string(&mut data).unwrap();
for line in data.lines() {
process(line);
};
}
51. @brian_pearce
fn read_file() -> Result<String, String> {
let mut data = String::new();
let mut f = try!(
File::open("my_file.txt").map_err(|e| e )
);
f.read_to_string(&mut data).unwrap();
for line in data.lines() {
process(line);
};
Ok(“Success”)
}
Rust
63. @brian_pearce
For the world was built to
develop character, and we
must learn that the
setbacks and grieves which
we endure help us in our
marching onward.
“
”
- Henry Ford