mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-16 07:36:05 +02:00
dprint fmt
This commit is contained in:
parent
4f94a87681
commit
e56715dd17
@ -15,9 +15,9 @@
|
||||
// ANCHOR: hello
|
||||
//! Rust <-> Java FFI demo.
|
||||
|
||||
use jni::JNIEnv;
|
||||
use jni::objects::{JClass, JString};
|
||||
use jni::sys::jstring;
|
||||
use jni::JNIEnv;
|
||||
|
||||
/// HelloWorld::hello method implementation.
|
||||
// SAFETY: There is no other global function of this name.
|
||||
|
@ -14,8 +14,8 @@
|
||||
|
||||
// ANCHOR: exceptions
|
||||
use log::error;
|
||||
use smccc::psci::system_off;
|
||||
use smccc::Hvc;
|
||||
use smccc::psci::system_off;
|
||||
|
||||
// SAFETY: There is no other global function of this name.
|
||||
#[unsafe(no_mangle)]
|
||||
|
@ -23,8 +23,8 @@ use crate::pl011::Uart;
|
||||
use core::fmt::Write;
|
||||
use core::panic::PanicInfo;
|
||||
use log::error;
|
||||
use smccc::psci::system_off;
|
||||
use smccc::Hvc;
|
||||
use smccc::psci::system_off;
|
||||
|
||||
/// Base address of the primary PL011 UART.
|
||||
const PL011_BASE_ADDRESS: *mut u32 = 0x900_0000 as _;
|
||||
|
@ -22,9 +22,9 @@ mod pl011;
|
||||
|
||||
use crate::pl011::Uart;
|
||||
use core::panic::PanicInfo;
|
||||
use log::{error, info, LevelFilter};
|
||||
use smccc::psci::system_off;
|
||||
use log::{LevelFilter, error, info};
|
||||
use smccc::Hvc;
|
||||
use smccc::psci::system_off;
|
||||
|
||||
/// Base address of the primary PL011 UART.
|
||||
const PL011_BASE_ADDRESS: *mut u32 = 0x900_0000 as _;
|
||||
|
@ -23,8 +23,8 @@ use crate::pl011_minimal::Uart;
|
||||
use core::fmt::Write;
|
||||
use core::panic::PanicInfo;
|
||||
use log::error;
|
||||
use smccc::psci::system_off;
|
||||
use smccc::Hvc;
|
||||
use smccc::psci::system_off;
|
||||
|
||||
/// Base address of the primary PL011 UART.
|
||||
const PL011_BASE_ADDRESS: *mut u8 = 0x900_0000 as _;
|
||||
|
@ -20,7 +20,7 @@ extern crate panic_halt as _;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use embedded_hal::digital::OutputPin;
|
||||
use nrf52833_hal::gpio::{p0, Level};
|
||||
use nrf52833_hal::gpio::{Level, p0};
|
||||
use nrf52833_hal::pac::Peripherals;
|
||||
|
||||
#[entry]
|
||||
|
@ -8,7 +8,7 @@ many elements are used and panics if you try to use more than are allocated.
|
||||
<!-- mdbook-xgettext: skip -->
|
||||
|
||||
```rust,editable,compile_fail
|
||||
use tinyvec::{array_vec, ArrayVec};
|
||||
use tinyvec::{ArrayVec, array_vec};
|
||||
|
||||
fn main() {
|
||||
let mut numbers: ArrayVec<[u32; 5]> = array_vec!(42, 66);
|
||||
|
@ -10,11 +10,7 @@ A closure can capture variables from the environment where it was defined.
|
||||
fn main() {
|
||||
let max_value = 5;
|
||||
let clamp = |v| {
|
||||
if v > max_value {
|
||||
max_value
|
||||
} else {
|
||||
v
|
||||
}
|
||||
if v > max_value { max_value } else { v }
|
||||
};
|
||||
|
||||
dbg!(clamp(1));
|
||||
|
@ -17,7 +17,7 @@ the resulting variables. The `statement` result becomes the result of the
|
||||
|
||||
```rust,editable,compile_fail
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::time::{sleep, Duration};
|
||||
use tokio::time::{Duration, sleep};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
@ -15,7 +15,7 @@
|
||||
// ANCHOR: solution
|
||||
// ANCHOR: Philosopher
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
use tokio::sync::{Mutex, mpsc};
|
||||
use tokio::time;
|
||||
|
||||
struct Chopstick;
|
||||
|
@ -22,7 +22,7 @@ with some caveats:
|
||||
```rust,editable,compile_fail
|
||||
use async_trait::async_trait;
|
||||
use std::time::Instant;
|
||||
use tokio::time::{sleep, Duration};
|
||||
use tokio::time::{Duration, sleep};
|
||||
|
||||
#[async_trait]
|
||||
trait Sleeper {
|
||||
|
@ -20,7 +20,7 @@ location.
|
||||
```rust,editable,compile_fail
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
use tokio::task::spawn;
|
||||
use tokio::time::{sleep, Duration};
|
||||
use tokio::time::{Duration, sleep};
|
||||
|
||||
// A work item. In this case, just sleep for the given time and respond
|
||||
// with a message on the `respond_on` channel.
|
||||
|
@ -8,11 +8,7 @@ minutes: 3
|
||||
|
||||
```rust,editable
|
||||
fn gcd(a: u32, b: u32) -> u32 {
|
||||
if b > 0 {
|
||||
gcd(b, a % b)
|
||||
} else {
|
||||
a
|
||||
}
|
||||
if b > 0 { gcd(b, a % b) } else { a }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -15,7 +15,7 @@ writing out trait impls explicitly for custom error types.
|
||||
[`thiserror`]: https://docs.rs/thiserror/
|
||||
|
||||
```rust,editable,compile_fail
|
||||
use anyhow::{bail, Context, Result};
|
||||
use anyhow::{Context, Result, bail};
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
use thiserror::Error;
|
||||
|
@ -26,12 +26,12 @@ use embedded_hal::digital::InputPin;
|
||||
use lsm303agr::{
|
||||
AccelMode, AccelOutputDataRate, Lsm303agr, MagMode, MagOutputDataRate,
|
||||
};
|
||||
use microbit::Board;
|
||||
use microbit::display::blocking::Display;
|
||||
use microbit::hal::twim::Twim;
|
||||
use microbit::hal::uarte::{Baudrate, Parity, Uarte};
|
||||
use microbit::hal::{Delay, Timer};
|
||||
use microbit::pac::twim0::frequency::FREQUENCY_A;
|
||||
use microbit::Board;
|
||||
|
||||
const COMPASS_SCALE: i32 = 30000;
|
||||
const ACCELEROMETER_SCALE: i32 = 700;
|
||||
|
@ -14,8 +14,8 @@
|
||||
|
||||
use arm_gic::gicv3::GicV3;
|
||||
use log::{error, info, trace};
|
||||
use smccc::psci::system_off;
|
||||
use smccc::Hvc;
|
||||
use smccc::psci::system_off;
|
||||
|
||||
// SAFETY: There is no other global function of this name.
|
||||
#[unsafe(no_mangle)]
|
||||
|
@ -24,16 +24,16 @@ mod pl011;
|
||||
mod pl031;
|
||||
|
||||
use crate::pl031::Rtc;
|
||||
use arm_gic::{irq_enable, wfi, IntId, Trigger};
|
||||
use arm_gic::{IntId, Trigger, irq_enable, wfi};
|
||||
use chrono::{TimeZone, Utc};
|
||||
use core::hint::spin_loop;
|
||||
// ANCHOR: imports
|
||||
use crate::pl011::Uart;
|
||||
use arm_gic::gicv3::GicV3;
|
||||
use core::panic::PanicInfo;
|
||||
use log::{error, info, trace, LevelFilter};
|
||||
use smccc::psci::system_off;
|
||||
use log::{LevelFilter, error, info, trace};
|
||||
use smccc::Hvc;
|
||||
use smccc::psci::system_off;
|
||||
|
||||
/// Base addresses of the GICv3.
|
||||
const GICD_BASE_ADDRESS: *mut u64 = 0x800_0000 as _;
|
||||
|
@ -9,11 +9,7 @@ Rust supports generics, which lets you abstract algorithms or data structures
|
||||
|
||||
```rust,editable
|
||||
fn pick<T>(cond: bool, left: T, right: T) -> T {
|
||||
if cond {
|
||||
left
|
||||
} else {
|
||||
right
|
||||
}
|
||||
if cond { left } else { right }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@ -31,19 +27,11 @@ fn main() {
|
||||
|
||||
```rust
|
||||
fn pick_i32(cond: bool, left: i32, right: i32) -> i32 {
|
||||
if cond {
|
||||
left
|
||||
} else {
|
||||
right
|
||||
}
|
||||
if cond { left } else { right }
|
||||
}
|
||||
|
||||
fn pick_char(cond: bool, left: char, right: char) -> char {
|
||||
if cond {
|
||||
left
|
||||
} else {
|
||||
right
|
||||
}
|
||||
if cond { left } else { right }
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -29,11 +29,7 @@ job.
|
||||
struct Point(i32, i32);
|
||||
|
||||
fn left_most(p1: &Point, p2: &Point) -> &Point {
|
||||
if p1.0 < p2.0 {
|
||||
p1
|
||||
} else {
|
||||
p2
|
||||
}
|
||||
if p1.0 < p2.0 { p1 } else { p2 }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -14,11 +14,7 @@ const DIGEST_SIZE: usize = 3;
|
||||
const FILL_VALUE: u8 = calculate_fill_value();
|
||||
|
||||
const fn calculate_fill_value() -> u8 {
|
||||
if DIGEST_SIZE < 10 {
|
||||
42
|
||||
} else {
|
||||
13
|
||||
}
|
||||
if DIGEST_SIZE < 10 { 42 } else { 13 }
|
||||
}
|
||||
|
||||
fn compute_digest(text: &str) -> [u8; DIGEST_SIZE] {
|
||||
|
@ -19,7 +19,7 @@
|
||||
//! `cargo xtask install-tools` and the logic defined here will install
|
||||
//! the tools.
|
||||
|
||||
use anyhow::{anyhow, Ok, Result};
|
||||
use anyhow::{Ok, Result, anyhow};
|
||||
use clap::Parser;
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
|
Loading…
x
Reference in New Issue
Block a user