(For reference: I know why it is the way it is, the stark difference is just kind of fun to contrast on trivial examples.)
a = input()
print(a)
fn main() {
// make a scope
let a = {
// we'll need a buffer to read into
let mut buf = String::new();
// won't bother with error handling here - just unwrap the Result
std::io::stdin().read_line(&mut buf).unwrap();
// get rid of Unicode whitespaces on ends of input
// and convert &str to String to get proper lifetimes
buf.trim().to_string()
};
println!("{}", &a);
}
a = "abc"
print(a[1])
fn main() {
let a = "abc";
// get nth element of characters iterator
// this might fail at runtime due to index being out of bounds
// therefore we unwrap
let b = a.chars().nth(1).unwrap();
println!("{}", &b);
}
from uniseg import graphemecluster
a = "a̐éö̲\r\n"
b = list(graphemecluster.grapheme_clusters(a))
print(b[1])
extern crate unicode_segmentation;
// UnicodeSegmentation is implemented for str
// therefore, using it gives our strs additional methods
use unicode_segmentation::UnicodeSegmentation;
fn main() {
let a = "a̐éö̲\r\n";
let b = &a.graphemes(true) // returns an iterator
.collect::<Vec<&str>>(); // collect it and specify type to cast to
println!("{}", &b[1]);
}
According to those examples, Rust doesn't seem easy 😆