hardDrop plus move interaction
struct Inner(&'static str);
impl Drop for Inner {
fn drop(&mut self) { println!("drop {}", self.0); }
}
struct Outer { a: Inner, b: Inner }
impl Drop for Outer {
fn drop(&mut self) { println!("drop Outer"); }
}
fn main() {
let _o = Outer { a: Inner("a"), b: Inner("b") };
}
Read the full question →easyiterators and adapters
fn main() {
let words = vec!["a", "bb", "ccc"];
let n = words.iter().map(|w| w.len()).filter(|&len| len > 1).count();
println!("{}", n);
}
Read the full question →easyownership and move
fn main() {
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);
}
Read the full question →easyownership and move
fn main() {
let x = 5;
let y = x;
println!("{} {}", x, y);
}
Read the full question →easyborrowing & and &mut
fn main() {
let s = String::from("hello");
let r = &mut s;
r.push('!');
}
Read the full question →easyderive macros
#[derive(Clone)]
struct Point { x: i32, y: i32 }
Read the full question →easyString vs &str
fn main() {
let mut s = String::from("hi");
s.push('!'); // line A
s.push("!!"); // line B
}
Read the full question →easytraits basics and impl
trait Greet {
fn hello(&self) -> String;
}
struct Dog;
fn main() {
let d = Dog;
println!("{}", d.hello());
}
Read the full question →easystructs and tuple structs
struct Point(i32, i32);
let p = Point(3, 7);
Read the full question →easymodules and use
mod math {
pub fn add(a: i32, b: i32) -> i32 { a + b }
}
fn main() {
// call add here
}
Read the full question →easyderive macros
#[derive(/* ? */)]
struct Color { r: u8, g: u8, b: u8 }
fn main() {
let a = Color { r: 1, g: 2, b: 3 };
let b = Color { r: 1, g: 2, b: 3 };
println!("{}", a == b);
}
Read the full question →easygenerics basics
fn largest<T>(list: &[T]) -> &T {
let mut largest = &list[0];
for item in list {
if item > largest {
largest = item;
}
}
largest
}
Read the full question →easymodules and use
mod util {
fn helper() -> i32 { 42 }
}
fn main() {
let x = util::helper();
}
Read the full question →easyString vs &str
fn main() {
let s = "abc";
let r: String = s.chars().rev().collect();
println!("{}", r);
}
Read the full question →easygenerics basics
fn first<T>(items: &[T]) -> &T {
&items[0]
}
fn main() {
let numbers = vec![10, 20, 30];
let x: &i32 = first(&numbers);
println!("{}", x);
}
Read the full question →easygenerics basics
struct Wrapper<T> {
a: T,
b: T,
}
let w = Wrapper { a: 5, b: 1.0 };
Read the full question →easyOption and ? on Option
fn first_char(s: &str) -> ??? {
let c = s.chars().next()?;
Some(c.to_ascii_uppercase())
}
Read the full question →mediumRc and Arc
use std::rc::{Rc, Weak};
struct Node {
parent: Weak<Node>,
children: Vec<Rc<Node>>,
}
Read the full question →easyResult and ? operator
fn parse_and_double(s: &str) -> Result<i32, std::num::ParseIntError> {
let n = s.parse::<i32>()?;
Ok(n * 2)
}
fn main() {
let r = parse_and_double("abc");
println!("{:?}", r);
}
Read the full question →mediumdefault and blanket impls
impl<T: Display> ToString for T {
fn to_string(&self) -> String { /* ... */ }
}
struct MyType;
impl ToString for MyType {
fn to_string(&self) -> String { String::from("custom") }
}
Read the full question →