init
This commit is contained in:
BIN
.idea/.author_contents_storage_db
generated
Normal file
BIN
.idea/.author_contents_storage_db
generated
Normal file
Binary file not shown.
BIN
.idea/.author_contents_storage_db-shm
generated
Normal file
BIN
.idea/.author_contents_storage_db-shm
generated
Normal file
Binary file not shown.
BIN
.idea/.author_contents_storage_db-wal
generated
Normal file
BIN
.idea/.author_contents_storage_db-wal
generated
Normal file
Binary file not shown.
10
.idea/.gitignore
generated
vendored
Normal file
10
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 已忽略包含查询文件的默认文件夹
|
||||
/queries/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
302
.idea/100 Exercises to Learn Rust.iml
generated
Normal file
302
.idea/100 Exercises to Learn Rust.iml
generated
Normal file
@@ -0,0 +1,302 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="EMPTY_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/BranchingIfElse/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/BranchingIfElse/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/BranchingIfElse/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/ConversionsAsCasting/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/ConversionsAsCasting/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/ConversionsAsCasting/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Factorial/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Factorial/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Factorial/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Integers/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Integers/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Integers/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Introduction/ABasicCalculatorIntroduction/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Introduction/ABasicCalculatorIntroduction/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/LoopsFor/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/LoopsFor/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/LoopsFor/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/LoopsWhile/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/LoopsWhile/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/LoopsWhile/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/OverflowAndUnderflow/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/OverflowAndUnderflow/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/OverflowAndUnderflow/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Panics/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Panics/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Panics/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/SaturatingArithmetic/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/SaturatingArithmetic/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/SaturatingArithmetic/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Variables/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Variables/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/ABasicCalculator/Variables/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/AsyncAwarePrimitives/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/AsyncAwarePrimitives/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/AsyncAwarePrimitives/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/AsynchronousFunctions/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/AsynchronousFunctions/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/AsynchronousFunctions/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/BlockingTheRuntime/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/BlockingTheRuntime/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/BlockingTheRuntime/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/Cancellation/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/Cancellation/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/Cancellation/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/FutureTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/FutureTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/FutureTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/Introduction/Futures - Introduction/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/Introduction/Futures - Introduction/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/Outro/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/Outro/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/Outro/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/Runtime/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/Runtime/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/Runtime/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/SpawningTasks/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/SpawningTasks/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Futures/SpawningTasks/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/GoingFurther/Epilogue/What next/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Introduction/GettingStarted/About/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Introduction/GettingStarted/CourseView/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Introduction/GettingStarted/Editor/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Introduction/GettingStarted/ExternalLinter/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Introduction/GettingStarted/HelloWorld/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Introduction/GettingStarted/HelloWorld/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Introduction/GettingStarted/NavigatingAround/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Introduction/GettingStarted/TaskDescription/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/AckPattern/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/AckPattern/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/AckPattern/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/BoundedChannels/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/BoundedChannels/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/BoundedChannels/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Channels/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Channels/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Channels/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Client/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Client/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Client/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/InteriorMutability/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/InteriorMutability/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/InteriorMutability/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Introduction/Threads - Introduction/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Introduction/Threads - Introduction/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/LeakingMemory/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/LeakingMemory/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/LeakingMemory/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/MutexSendAndArc/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/MutexSendAndArc/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/MutexSendAndArc/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Patching/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Patching/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Patching/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/RwLock/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/RwLock/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/RwLock/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/ScopedThreads/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/ScopedThreads/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/ScopedThreads/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/StaticLifetime/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/StaticLifetime/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/StaticLifetime/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/SyncTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/SyncTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/SyncTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Threads/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Threads/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/Threads/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/WithoutChannels/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/WithoutChannels/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Threads/WithoutChannels/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Arrays/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Arrays/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Arrays/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/BTreeMap/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/BTreeMap/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/BTreeMap/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Combinators/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Combinators/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Combinators/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/HashMap/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/HashMap/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/HashMap/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/ImplTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/ImplTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/ImplTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/ImplTraitPt2/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/ImplTraitPt2/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/ImplTraitPt2/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/IndexMutTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/IndexMutTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/IndexMutTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/IndexTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/IndexTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/IndexTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Introduction/Ticket Management - Introduction/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Introduction/Ticket Management - Introduction/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Iter/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Iter/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Iter/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Iterators/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Iterators/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Iterators/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Lifetimes/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Lifetimes/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Lifetimes/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/MutableSlices/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/MutableSlices/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/MutableSlices/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Resizing/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Resizing/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Resizing/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Slices/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Slices/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Slices/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/TwoStates/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/TwoStates/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/TwoStates/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Vectors/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Vectors/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketManagement/Vectors/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Destructors/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Destructors/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Destructors/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Encapsulation/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Encapsulation/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Encapsulation/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Heap/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Heap/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Heap/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Introduction/Ticket v1 - Introduction/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Introduction/Ticket v1 - Introduction/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Modules/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Modules/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Modules/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Outro/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Outro/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Outro/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Ownership/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Ownership/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Ownership/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/ReferencesInMemory/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/ReferencesInMemory/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/ReferencesInMemory/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Setters/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Setters/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Setters/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Stack/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Stack/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Stack/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Structs/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Structs/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Structs/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Validation/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Validation/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Validation/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Visibility/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Visibility/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV1/Visibility/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/BranchingIfLetAndLetElse/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/BranchingIfLetAndLetElse/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/BranchingIfLetAndLetElse/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/BranchingMatch/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/BranchingMatch/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/BranchingMatch/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Dependencies/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Dependencies/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Dependencies/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Enums/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Enums/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Enums/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/ErrorEnums/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/ErrorEnums/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/ErrorEnums/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/ErrorSource/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/ErrorSource/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/ErrorSource/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/ErrorTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/ErrorTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/ErrorTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Fallibility/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Fallibility/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Fallibility/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Introduction/Ticket v2 - Introduction/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Introduction/Ticket v2 - Introduction/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Nullability/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Nullability/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Nullability/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Outro/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Outro/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Outro/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Packages/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Packages/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Packages/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/TryFromTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/TryFromTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/TryFromTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Unwrap/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Unwrap/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/Unwrap/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/VariantsWithData/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/VariantsWithData/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/VariantsWithData/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/thiserror/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/thiserror/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/TicketV2/thiserror/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/AssociatedVsGenericTypes/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/AssociatedVsGenericTypes/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/AssociatedVsGenericTypes/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/CloneTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/CloneTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/CloneTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/CopyTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/CopyTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/CopyTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/DerefTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/DerefTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/DerefTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/DeriveMacros/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/DeriveMacros/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/DeriveMacros/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/DropTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/DropTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/DropTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/FromTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/FromTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/FromTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/Introduction/Traits - Introduction/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/Introduction/Traits - Introduction/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/OperatorOverloading/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/OperatorOverloading/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/OperatorOverloading/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/OrphanRule/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/OrphanRule/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/OrphanRule/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/Outro/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/Outro/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/Outro/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/SizedTrait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/SizedTrait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/SizedTrait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/StringSlices/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/StringSlices/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/StringSlices/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/Trait/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/Trait/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/Trait/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/TraitBounds/Task/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/TraitBounds/Task/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/Traits/TraitBounds/Theory/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/helpers/common/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/helpers/ticket_fields/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
7
.idea/externalDependencies.xml
generated
Normal file
7
.idea/externalDependencies.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalDependencies">
|
||||
<plugin id="com.jetbrains.rust" />
|
||||
<plugin id="org.toml.lang" />
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/100 Exercises to Learn Rust.iml" filepath="$PROJECT_DIR$/.idea/100 Exercises to Learn Rust.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
4
ABasicCalculator/BranchingIfElse/Task/Cargo.toml
Normal file
4
ABasicCalculator/BranchingIfElse/Task/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "task_branching"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
14
ABasicCalculator/BranchingIfElse/Task/src/lib.rs
Normal file
14
ABasicCalculator/BranchingIfElse/Task/src/lib.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
/// Return `12` if `n` is even,
|
||||
/// `13` if `n` is divisible by `3`,
|
||||
/// `17` otherwise.
|
||||
pub fn magic_number(n: u32) -> u32 {
|
||||
if n % 2 == 0 {
|
||||
12
|
||||
}else {
|
||||
if n % 3 == 0 {
|
||||
13
|
||||
}else {
|
||||
17
|
||||
}
|
||||
}
|
||||
}
|
||||
27
ABasicCalculator/BranchingIfElse/Task/task-info.yaml
Normal file
27
ABasicCalculator/BranchingIfElse/Task/task-info.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
type: edu
|
||||
files:
|
||||
- name: src/lib.rs
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 130
|
||||
length: 124
|
||||
placeholder_text: /*TODO*/
|
||||
initial_state:
|
||||
length: 8
|
||||
offset: 130
|
||||
initialized_from_dependency: false
|
||||
encrypted_possible_answer: rU7BVkr6lh0wIe5YDtA/A+e3+1BNLrsCW2VgoPzfdPF5P2Ia2xJxNciLcRMq74me52Iw6Lw9hxW5PKwStenwlctnVW29NoM3m46gINTf69np4DKVU88oj56TzKcVvLao
|
||||
selected: true
|
||||
status: Solved
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
- name: tests/tests.rs
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Solved
|
||||
feedback:
|
||||
message: 恭喜!
|
||||
time: "Tue, 24 Feb 2026 08:31:17 UTC"
|
||||
record: -1
|
||||
@@ -0,0 +1 @@
|
||||
id: 839362726
|
||||
2
ABasicCalculator/BranchingIfElse/Task/task.md
Normal file
2
ABasicCalculator/BranchingIfElse/Task/task.md
Normal file
@@ -0,0 +1,2 @@
|
||||
This task involves implementing the `magic_number` function.
|
||||
The `magic_number` function takes an unsigned `32-bit` integer `n` as input and should return a `u32` based on the rules in code comment.
|
||||
4
ABasicCalculator/BranchingIfElse/Theory/Cargo.toml
Normal file
4
ABasicCalculator/BranchingIfElse/Theory/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "theory_branching"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
3
ABasicCalculator/BranchingIfElse/Theory/src/main.rs
Normal file
3
ABasicCalculator/BranchingIfElse/Theory/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
// put your code here to launch it
|
||||
}
|
||||
11
ABasicCalculator/BranchingIfElse/Theory/task-info.yaml
Normal file
11
ABasicCalculator/BranchingIfElse/Theory/task-info.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
type: theory
|
||||
files:
|
||||
- name: src/main.rs
|
||||
visible: true
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Solved
|
||||
record: -1
|
||||
post_submission_on_open: true
|
||||
@@ -0,0 +1 @@
|
||||
id: 48107198
|
||||
133
ABasicCalculator/BranchingIfElse/Theory/task.md
Normal file
133
ABasicCalculator/BranchingIfElse/Theory/task.md
Normal file
@@ -0,0 +1,133 @@
|
||||
## Control flow
|
||||
|
||||
All our programs so far have been pretty straightforward.\
|
||||
A sequence of instructions is executed from top to bottom, and that's it.
|
||||
|
||||
It's time to introduce some **branching**.
|
||||
|
||||
## `if` clauses
|
||||
|
||||
The `if` keyword is used to execute a block of code only if a condition is true.
|
||||
|
||||
Here's a simple example:
|
||||
|
||||
```rust
|
||||
let number = 3;
|
||||
if number < 5 {
|
||||
println!("`number` is smaller than 5");
|
||||
}
|
||||
```
|
||||
|
||||
This program will print `number is smaller than 5` because the condition `number < 5` is true.
|
||||
|
||||
### `else` clauses
|
||||
|
||||
Like most programming languages, Rust supports an optional `else` branch to execute a block of code when the condition in an
|
||||
`if` expression is false.\
|
||||
For example:
|
||||
|
||||
```rust
|
||||
let number = 3;
|
||||
|
||||
if number < 5 {
|
||||
println!("`number` is smaller than 5");
|
||||
} else {
|
||||
println!("`number` is greater than or equal to 5");
|
||||
}
|
||||
```
|
||||
|
||||
### `else if` clauses
|
||||
|
||||
Your code drifts more and more to the right when you have multiple `if` expressions, one nested inside the other.
|
||||
|
||||
```rust
|
||||
let number = 3;
|
||||
|
||||
if number < 5 {
|
||||
println!("`number` is smaller than 5");
|
||||
} else {
|
||||
if number >= 3 {
|
||||
println!("`number` is greater than or equal to 3, but smaller than 5");
|
||||
} else {
|
||||
println!("`number` is smaller than 3");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You can use the `else if` keyword to combine multiple `if` expressions into a single one:
|
||||
|
||||
```rust
|
||||
let number = 3;
|
||||
|
||||
if number < 5 {
|
||||
println!("`number` is smaller than 5");
|
||||
} else if number >= 3 {
|
||||
println!("`number` is greater than or equal to 3, but smaller than 5");
|
||||
} else {
|
||||
println!("`number` is smaller than 3");
|
||||
}
|
||||
```
|
||||
|
||||
## Booleans
|
||||
|
||||
The condition in an `if` expression must be of type `bool`, a **boolean**.\
|
||||
Booleans, just like integers, are a primitive type in Rust.
|
||||
|
||||
A boolean can have one of two values: `true` or `false`.
|
||||
|
||||
### No truthy or falsy values
|
||||
|
||||
If the condition in an `if` expression is not a boolean, you'll get a compilation error.
|
||||
|
||||
For example, the following code will not compile:
|
||||
|
||||
```rust
|
||||
let number = 3;
|
||||
if number {
|
||||
println!("`number` is not zero");
|
||||
}
|
||||
```
|
||||
|
||||
You'll get the following compilation error:
|
||||
|
||||
```text
|
||||
error[E0308]: mismatched types
|
||||
--> src/main.rs:3:8
|
||||
|
|
||||
3 | if number {
|
||||
| ^^^^^^ expected `bool`, found integer
|
||||
```
|
||||
|
||||
This follows from Rust's philosophy around type coercion: there's no automatic conversion from non-boolean types to booleans.
|
||||
Rust doesn't have the concept of **truthy** or **falsy** values, like JavaScript or Python.\
|
||||
You have to be explicit about the condition you want to check.
|
||||
|
||||
### Comparison operators
|
||||
|
||||
It's quite common to use comparison operators to build conditions for `if` expressions.\
|
||||
Here are the comparison operators available in Rust when working with integers:
|
||||
|
||||
- `==`: equal to
|
||||
- `!=`: not equal to
|
||||
- `<`: less than
|
||||
- `>`: greater than
|
||||
- `<=`: less than or equal to
|
||||
- `>=`: greater than or equal to
|
||||
|
||||
## `if/else` is an expression
|
||||
|
||||
In Rust, `if` expressions are **expressions**, not statements: they return a value.\
|
||||
That value can be assigned to a variable or used in other expressions. For example:
|
||||
|
||||
```rust
|
||||
let number = 3;
|
||||
let message = if number < 5 {
|
||||
"smaller than 5"
|
||||
} else {
|
||||
"greater than or equal to 5"
|
||||
};
|
||||
```
|
||||
|
||||
In the example above, each branch of the `if` evaluates to a string literal,
|
||||
which is then assigned to the `message` variable.\
|
||||
The only requirement is that both `if` branches return the same type.
|
||||
4
ABasicCalculator/BranchingIfElse/lesson-info.yaml
Normal file
4
ABasicCalculator/BranchingIfElse/lesson-info.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
custom_name: Branching if-else
|
||||
content:
|
||||
- Theory
|
||||
- Task
|
||||
1
ABasicCalculator/BranchingIfElse/lesson-remote-info.yaml
Normal file
1
ABasicCalculator/BranchingIfElse/lesson-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 822833509
|
||||
4
ABasicCalculator/ConversionsAsCasting/Task/Cargo.toml
Normal file
4
ABasicCalculator/ConversionsAsCasting/Task/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "task_conversions_as_casting"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
1
ABasicCalculator/ConversionsAsCasting/Task/src/lib.rs
Normal file
1
ABasicCalculator/ConversionsAsCasting/Task/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
44
ABasicCalculator/ConversionsAsCasting/Task/task-info.yaml
Normal file
44
ABasicCalculator/ConversionsAsCasting/Task/task-info.yaml
Normal file
@@ -0,0 +1,44 @@
|
||||
type: edu
|
||||
files:
|
||||
- name: src/lib.rs
|
||||
visible: false
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
- name: tests/tests.rs
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 81
|
||||
length: 10
|
||||
placeholder_text: /* TODO */
|
||||
initial_state:
|
||||
length: 10
|
||||
offset: 81
|
||||
initialized_from_dependency: false
|
||||
encrypted_possible_answer: 4w33P5f+Ces/HqPWq0IorA==
|
||||
selected: false
|
||||
status: Unchecked
|
||||
- offset: 920
|
||||
length: 10
|
||||
placeholder_text: /* TODO */
|
||||
initial_state:
|
||||
length: 10
|
||||
offset: 920
|
||||
initialized_from_dependency: false
|
||||
encrypted_possible_answer: RESya3fqSV1DFqjsTgSSYQ==
|
||||
selected: false
|
||||
status: Unchecked
|
||||
- offset: 1020
|
||||
length: 10
|
||||
placeholder_text: /* TODO */
|
||||
initial_state:
|
||||
length: 10
|
||||
offset: 1020
|
||||
initialized_from_dependency: false
|
||||
encrypted_possible_answer: oa+nVSRiKi7L3SNrfCrSCA==
|
||||
selected: false
|
||||
status: Unchecked
|
||||
learner_created: false
|
||||
status: Unchecked
|
||||
record: -1
|
||||
@@ -0,0 +1 @@
|
||||
id: 1735357693
|
||||
1
ABasicCalculator/ConversionsAsCasting/Task/task.md
Normal file
1
ABasicCalculator/ConversionsAsCasting/Task/task.md
Normal file
@@ -0,0 +1 @@
|
||||
This task is to replace TODOs in the tests with the correct values for explicit type conversions, applying Rust's casting rules.
|
||||
34
ABasicCalculator/ConversionsAsCasting/Task/tests/tests.rs
Normal file
34
ABasicCalculator/ConversionsAsCasting/Task/tests/tests.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
#[test]
|
||||
fn u16_to_u32() {
|
||||
let v: u32 = /* TODO */;
|
||||
assert_eq!(47u16 as u32, v);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn u8_to_i8() {
|
||||
// The compiler is smart enough to know that the value 255 cannot fit
|
||||
// inside an i8, so it'll emit a hard error. We intentionally disable
|
||||
// this guardrail to make this (bad) conversion possible.
|
||||
// The compiler is only able to pick on this because the value is a
|
||||
// literal. If we were to use a variable, the compiler wouldn't be able to
|
||||
// catch this at compile time.
|
||||
#[allow(overflowing_literals)]
|
||||
let x = { 255 as i8 };
|
||||
|
||||
// You could solve this by using exactly the same expression as above,
|
||||
// but that would defeat the purpose of the exercise. Instead, use a genuine
|
||||
// `i8` value that is equivalent to `255` when converted from `u8`.
|
||||
let y: i8 = /* TODO */;
|
||||
|
||||
assert_eq!(x, y);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bool_to_u8() {
|
||||
let v: u8 = /* TODO */;
|
||||
assert_eq!(true as u8, v);
|
||||
}
|
||||
}
|
||||
4
ABasicCalculator/ConversionsAsCasting/Theory/Cargo.toml
Normal file
4
ABasicCalculator/ConversionsAsCasting/Theory/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "theory_conversions_as_casting"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
3
ABasicCalculator/ConversionsAsCasting/Theory/src/main.rs
Normal file
3
ABasicCalculator/ConversionsAsCasting/Theory/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
// put your code here to launch it
|
||||
}
|
||||
11
ABasicCalculator/ConversionsAsCasting/Theory/task-info.yaml
Normal file
11
ABasicCalculator/ConversionsAsCasting/Theory/task-info.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
type: theory
|
||||
files:
|
||||
- name: src/main.rs
|
||||
visible: true
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Unchecked
|
||||
record: -1
|
||||
post_submission_on_open: true
|
||||
@@ -0,0 +1 @@
|
||||
id: 1217457677
|
||||
102
ABasicCalculator/ConversionsAsCasting/Theory/task.md
Normal file
102
ABasicCalculator/ConversionsAsCasting/Theory/task.md
Normal file
@@ -0,0 +1,102 @@
|
||||
## Conversions, `as` casting
|
||||
|
||||
We've repeated over and over again that Rust won't perform
|
||||
implicit type conversions for integers.\
|
||||
How do you perform _explicit_ conversions then?
|
||||
|
||||
## `as`
|
||||
|
||||
You can use the `as` operator to convert between integer types.\
|
||||
`as` conversions are **infallible**.
|
||||
|
||||
For example:
|
||||
|
||||
```rust
|
||||
let a: u32 = 10;
|
||||
|
||||
// Cast `a` into the `u64` type
|
||||
let b = a as u64;
|
||||
|
||||
// You can use `_` as the target type
|
||||
// if it can be correctly inferred
|
||||
// by the compiler. For example:
|
||||
let c: u64 = a as _;
|
||||
```
|
||||
|
||||
The semantics of this conversion are what you expect: all `u32` values are valid `u64`
|
||||
values.
|
||||
|
||||
### Truncation
|
||||
|
||||
Things get more interesting if we go in the opposite direction:
|
||||
|
||||
```rust
|
||||
// A number that's too big
|
||||
// to fit into a `u8`
|
||||
let a: u16 = 255 + 1;
|
||||
let b = a as u8;
|
||||
```
|
||||
|
||||
This program will run without issues, because `as` conversions are infallible.
|
||||
But what is the value of `b`?
|
||||
When going from a larger integer type to a smaller, the Rust compiler will perform
|
||||
a **truncation**.
|
||||
|
||||
To understand what happens, let's start by looking at how `256u16` is
|
||||
represented in memory, as a sequence of bits:
|
||||
|
||||
```text
|
||||
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
|
||||
| | |
|
||||
+---------------+---------------+
|
||||
First 8 bits Last 8 bits
|
||||
```
|
||||
|
||||
When converting to a `u8`, the Rust compiler will keep the last 8 bits of a `u16`
|
||||
memory representation:
|
||||
|
||||
```text
|
||||
0 0 0 0 0 0 0 0
|
||||
| |
|
||||
+---------------+
|
||||
Last 8 bits
|
||||
```
|
||||
|
||||
Hence `256 as u8` is equal to `0`. That's... not ideal, in most scenarios.\
|
||||
In fact, the Rust compiler will actively try to stop you if it sees you trying
|
||||
to cast a literal value which will result in a truncation:
|
||||
|
||||
```text
|
||||
error: literal out of range for `i8`
|
||||
|
|
||||
4 | let a = 255 as i8;
|
||||
| ^^^
|
||||
|
|
||||
= note: the literal `255` does not fit into the type `i8`
|
||||
whose range is `-128..=127`
|
||||
= help: consider using the type `u8` instead
|
||||
= note: `#[deny(overflowing_literals)]` on by default
|
||||
```
|
||||
|
||||
### Recommendation
|
||||
|
||||
As a rule of thumb, be quite careful with `as` casting.\
|
||||
Use it _exclusively_ for going from a smaller type to a larger type.
|
||||
To convert from a larger to smaller integer type, rely on the
|
||||
[_fallible_ conversion machinery](../../../Ticket%20v2/TryFrom%20trait/Theory/task.md) that we'll
|
||||
explore later in the course.
|
||||
|
||||
### Limitations
|
||||
|
||||
Surprising behaviour is not the only downside of `as` casting.
|
||||
It is also fairly limited: you can only rely on `as` casting
|
||||
for primitive types and a few other special cases.\
|
||||
When working with composite types, you'll have to rely on
|
||||
different conversion mechanisms ([fallible](../../../Ticket%20v2/TryFrom%20trait/Theory/task.md)
|
||||
and [infallible](../../../Traits/From%20trait/Theory/task.md), which we'll explore later on.
|
||||
|
||||
## Further reading
|
||||
|
||||
- Check out [Rust's official reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#numeric-cast)
|
||||
to learn the precise behaviour of `as` casting for each source/target combination,
|
||||
as well as the exhaustive list of allowed conversions.
|
||||
4
ABasicCalculator/ConversionsAsCasting/lesson-info.yaml
Normal file
4
ABasicCalculator/ConversionsAsCasting/lesson-info.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
custom_name: Conversions - as casting
|
||||
content:
|
||||
- Theory
|
||||
- Task
|
||||
@@ -0,0 +1 @@
|
||||
id: 3739315
|
||||
4
ABasicCalculator/Factorial/Task/Cargo.toml
Normal file
4
ABasicCalculator/Factorial/Task/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "task_factorial"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
19
ABasicCalculator/Factorial/Task/src/lib.rs
Normal file
19
ABasicCalculator/Factorial/Task/src/lib.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
// Define a function named `factorial` that, given a non-negative integer `n`,
|
||||
// returns `n!`, the factorial of `n`.
|
||||
//
|
||||
// The factorial of `n` is defined as the product of all positive integers up to `n`.
|
||||
// For example, `5!` (read "five factorial") is `5 * 4 * 3 * 2 * 1`, which is `120`.
|
||||
// `0!` is defined to be `1`.
|
||||
//
|
||||
// We expect `factorial(0)` to return `1`, `factorial(1)` to return `1`,
|
||||
// `factorial(2)` to return `2`, and so on.
|
||||
//
|
||||
// Use only what you learned! No loops yet, so you'll have to use recursion!
|
||||
|
||||
pub fn factorial(n: u32) -> u32 {
|
||||
if n != 0 {
|
||||
n * factorial(n - 1)
|
||||
}else {
|
||||
1
|
||||
}
|
||||
}
|
||||
27
ABasicCalculator/Factorial/Task/task-info.yaml
Normal file
27
ABasicCalculator/Factorial/Task/task-info.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
type: edu
|
||||
files:
|
||||
- name: src/lib.rs
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 523
|
||||
length: 108
|
||||
placeholder_text: /* TODO */
|
||||
initial_state:
|
||||
length: 10
|
||||
offset: 523
|
||||
initialized_from_dependency: false
|
||||
encrypted_possible_answer: VcN7n8pN4juX6JH6PuAoIET4D3fNlzyc+WYl3i8r9MHkJ6iLk8VkX3L1do206H7+S6ZfqVHEv28mHnLz+wsPev/UUThmXE/wtS0Ip/cM9NGimnr3L5b7m4V9IVVtJlmVfNpuaGBBnsEQ+vZO/YVaqQ==
|
||||
selected: true
|
||||
status: Solved
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
- name: tests/tests.rs
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Solved
|
||||
feedback:
|
||||
message: 恭喜!
|
||||
time: "Tue, 24 Feb 2026 08:50:18 UTC"
|
||||
record: -1
|
||||
1
ABasicCalculator/Factorial/Task/task-remote-info.yaml
Normal file
1
ABasicCalculator/Factorial/Task/task-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 2112961728
|
||||
2
ABasicCalculator/Factorial/Task/task.md
Normal file
2
ABasicCalculator/Factorial/Task/task.md
Normal file
@@ -0,0 +1,2 @@
|
||||
This task is to implement the `factorial` function.
|
||||
As instructed by the comments in the code, you must calculate `n!` for a non-negative integer `n` using recursion, without using loops.
|
||||
0
ABasicCalculator/Factorial/Task/tests/tests.rs
Normal file
0
ABasicCalculator/Factorial/Task/tests/tests.rs
Normal file
4
ABasicCalculator/Factorial/Theory/Cargo.toml
Normal file
4
ABasicCalculator/Factorial/Theory/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "theory_factorial"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
3
ABasicCalculator/Factorial/Theory/src/main.rs
Normal file
3
ABasicCalculator/Factorial/Theory/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
// put your code here to launch it
|
||||
}
|
||||
11
ABasicCalculator/Factorial/Theory/task-info.yaml
Normal file
11
ABasicCalculator/Factorial/Theory/task-info.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
type: theory
|
||||
files:
|
||||
- name: src/main.rs
|
||||
visible: true
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Solved
|
||||
record: -1
|
||||
post_submission_on_open: true
|
||||
1
ABasicCalculator/Factorial/Theory/task-remote-info.yaml
Normal file
1
ABasicCalculator/Factorial/Theory/task-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 1963166623
|
||||
11
ABasicCalculator/Factorial/Theory/task.md
Normal file
11
ABasicCalculator/Factorial/Theory/task.md
Normal file
@@ -0,0 +1,11 @@
|
||||
## Factorial
|
||||
##
|
||||
So far you've learned:
|
||||
|
||||
- How to define a function
|
||||
- How to call a function
|
||||
- Which integer types are available in Rust
|
||||
- Which arithmetic operators are available for integers
|
||||
- How to execute conditional logic via comparisons and `if`/`else` expressions
|
||||
|
||||
It looks like you're ready to tackle factorials!
|
||||
3
ABasicCalculator/Factorial/lesson-info.yaml
Normal file
3
ABasicCalculator/Factorial/lesson-info.yaml
Normal file
@@ -0,0 +1,3 @@
|
||||
content:
|
||||
- Theory
|
||||
- Task
|
||||
1
ABasicCalculator/Factorial/lesson-remote-info.yaml
Normal file
1
ABasicCalculator/Factorial/lesson-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 1811428691
|
||||
4
ABasicCalculator/Integers/Task/Cargo.toml
Normal file
4
ABasicCalculator/Integers/Task/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "task_integers"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
5
ABasicCalculator/Integers/Task/src/lib.rs
Normal file
5
ABasicCalculator/Integers/Task/src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
pub fn compute(a: u32, b: u32) -> u32 {
|
||||
// TODO: change the line below to fix the compiler error and make the tests pass.
|
||||
let multiplier: u32 = 4;
|
||||
a + b * multiplier
|
||||
}
|
||||
27
ABasicCalculator/Integers/Task/task-info.yaml
Normal file
27
ABasicCalculator/Integers/Task/task-info.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
type: edu
|
||||
files:
|
||||
- name: src/lib.rs
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 146
|
||||
length: 3
|
||||
placeholder_text: /*TODO*/
|
||||
initial_state:
|
||||
length: 8
|
||||
offset: 146
|
||||
initialized_from_dependency: false
|
||||
encrypted_possible_answer: tW4+HiJ7rgGNCq5MZ0RUQQ==
|
||||
selected: false
|
||||
status: Solved
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
- name: tests/tests.rs
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Solved
|
||||
feedback:
|
||||
message: 恭喜!
|
||||
time: "Tue, 24 Feb 2026 07:19:08 UTC"
|
||||
record: -1
|
||||
1
ABasicCalculator/Integers/Task/task-remote-info.yaml
Normal file
1
ABasicCalculator/Integers/Task/task-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 829886820
|
||||
2
ABasicCalculator/Integers/Task/task.md
Normal file
2
ABasicCalculator/Integers/Task/task.md
Normal file
@@ -0,0 +1,2 @@
|
||||
This task is to modify the `compute` function to resolve a compiler error and allow tests to pass.
|
||||
Check `TODO` comment in the code.
|
||||
0
ABasicCalculator/Integers/Task/tests/tests.rs
Normal file
0
ABasicCalculator/Integers/Task/tests/tests.rs
Normal file
4
ABasicCalculator/Integers/Theory/Cargo.toml
Normal file
4
ABasicCalculator/Integers/Theory/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "theory_integers"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
3
ABasicCalculator/Integers/Theory/src/main.rs
Normal file
3
ABasicCalculator/Integers/Theory/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
// put your code here to launch it
|
||||
}
|
||||
11
ABasicCalculator/Integers/Theory/task-info.yaml
Normal file
11
ABasicCalculator/Integers/Theory/task-info.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
type: theory
|
||||
files:
|
||||
- name: src/main.rs
|
||||
visible: true
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Solved
|
||||
record: -1
|
||||
post_submission_on_open: true
|
||||
1
ABasicCalculator/Integers/Theory/task-remote-info.yaml
Normal file
1
ABasicCalculator/Integers/Theory/task-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 2019333427
|
||||
129
ABasicCalculator/Integers/Theory/task.md
Normal file
129
ABasicCalculator/Integers/Theory/task.md
Normal file
@@ -0,0 +1,129 @@
|
||||
## Primitive types
|
||||
|
||||
`u32` is one of Rust's **primitive types**. Primitive types are the most basic building blocks of a language.
|
||||
They're built into the language itself—i.e. they are not defined in terms of other types.
|
||||
|
||||
You can combine these primitive types to create more complex types. We'll see how soon enough.
|
||||
|
||||
## Integers
|
||||
|
||||
`u32`, in particular, is an **unsigned 32-bit integer**.
|
||||
|
||||
An integer is a number that can be written without a fractional component. E.g. `1` is an integer, while `1.2` is not.
|
||||
|
||||
### Signed vs. unsigned
|
||||
|
||||
An integer can be **signed** or **unsigned**.\
|
||||
An unsigned integer can only represent non-negative numbers (i.e. `0` or greater).
|
||||
A signed integer can represent both positive and negative numbers (e.g. `-1`, `12`, etc.).
|
||||
|
||||
The `u` in `u32` stands for **unsigned**.\
|
||||
The equivalent type for signed integer is `i32`, where the `i` stands for integer (i.e. any integer, positive or
|
||||
negative).
|
||||
|
||||
### Bit width
|
||||
|
||||
The `32` in `u32` refers to the **number of bits[^bit]** used to represent the number in memory.\
|
||||
The more bits, the larger the range of numbers that can be represented.
|
||||
|
||||
Rust supports multiple bit widths for integers: `8`, `16`, `32`, `64`, `128`.
|
||||
|
||||
With 32 bits, `u32` can represent numbers from `0` to `2^32 - 1` (a.k.a. [`u32::MAX`](https://doc.rust-lang.org/std/primitive.u32.html#associatedconstant.MAX)).\
|
||||
With the same number of bits, a signed integer (`i32`) can represent numbers from `-2^31` to `2^31 - 1`
|
||||
(i.e. from [`i32::MIN`](https://doc.rust-lang.org/std/primitive.i32.html#associatedconstant.MIN)
|
||||
to [`i32::MAX`](https://doc.rust-lang.org/std/primitive.i32.html#associatedconstant.MAX)).\
|
||||
The maximum value for `i32` is smaller than the maximum value for `u32` because one bit is used to represent
|
||||
the sign of the number. Check out the [two's complement](https://en.wikipedia.org/wiki/Two%27s_complement)
|
||||
representation for more details on how signed integers are represented in memory.
|
||||
|
||||
### Summary
|
||||
|
||||
Combining the two variables (signed/unsigned and bit width), we get the following integer types:
|
||||
|
||||
| Bit width | Signed | Unsigned |
|
||||
| --------- | ------ | -------- |
|
||||
| 8-bit | `i8` | `u8` |
|
||||
| 16-bit | `i16` | `u16` |
|
||||
| 32-bit | `i32` | `u32` |
|
||||
| 64-bit | `i64` | `u64` |
|
||||
| 128-bit | `i128` | `u128` |
|
||||
|
||||
## Literals
|
||||
|
||||
A **literal** is a notation for representing a fixed value in source code.\
|
||||
For example, `42` is a Rust literal for the number forty-two.
|
||||
|
||||
### Type annotations for literals
|
||||
|
||||
But all values in Rust have a type, so... what's the type of `42`?
|
||||
|
||||
The Rust compiler will try to infer the type of a literal based on how it's used.\
|
||||
If you don't provide any context, the compiler will default to `i32` for integer literals.\
|
||||
If you want to use a different type, you can add the desired integer type as a suffix—e.g. `2u64` is a 2 that's
|
||||
explicitly typed as a `u64`.
|
||||
|
||||
### Underscores in literals
|
||||
|
||||
You can use underscores `_` to improve the readability of large numbers.\
|
||||
For example, `1_000_000` is the same as `1000000`.
|
||||
|
||||
## Arithmetic operators
|
||||
|
||||
Rust supports the following arithmetic operators[^traits] for integers:
|
||||
|
||||
- `+` for addition
|
||||
- `-` for subtraction
|
||||
- `*` for multiplication
|
||||
- `/` for division
|
||||
- `%` for remainder
|
||||
|
||||
Precedence and associativity rules for these operators are the same as in mathematics.\
|
||||
You can use parentheses to override the default precedence. E.g. `2 * (3 + 4)`.
|
||||
|
||||
> ⚠️ **Warning**
|
||||
>
|
||||
> The division operator `/` performs integer division when used with integer types.
|
||||
> I.e. the result is truncated towards zero. For example, `5 / 2` is `2`, not `2.5`.
|
||||
|
||||
## No automatic type coercion
|
||||
|
||||
As we discussed in the previous exercise, Rust is a statically typed language.\
|
||||
In particular, Rust is quite strict about type coercion. It won't automatically convert a value from one type to
|
||||
another[^coercion],
|
||||
even if the conversion is lossless. You have to do it explicitly.
|
||||
|
||||
For example, you can't assign a `u8` value to a variable with type `u32`, even though all `u8` values are valid `u32`
|
||||
values:
|
||||
|
||||
```rust
|
||||
let b: u8 = 100;
|
||||
let a: u32 = b;
|
||||
```
|
||||
|
||||
It'll throw a compilation error:
|
||||
|
||||
```text
|
||||
error[E0308]: mismatched types
|
||||
|
|
||||
3 | let a: u32 = b;
|
||||
| --- ^ expected `u32`, found `u8`
|
||||
| |
|
||||
| expected due to this
|
||||
|
|
||||
```
|
||||
|
||||
We'll see how to convert between types [later in this course](../../../Traits/From%20trait/Theory/task.md)
|
||||
|
||||
## Further reading
|
||||
|
||||
- [The integer types section](https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-types) in the official Rust book
|
||||
|
||||
[^bit]: A bit is the smallest unit of data in a computer. It can only have two values: `0` or `1`.
|
||||
|
||||
[^traits]: Rust doesn't let you define custom operators, but it puts you in control of how the built-in operators
|
||||
behave.
|
||||
We'll talk about operator overloading [later in the course](../../../Traits/Operator%20overloading/Theory/task.md), after we've covered traits.
|
||||
|
||||
[^coercion]: There are some exceptions to this rule, mostly related to references, smart pointers and ergonomics. We'll
|
||||
cover those [later on](../../../Traits/Deref%20trait/Theory/task.md).
|
||||
A mental model of "all conversions are explicit" will serve you well in the meantime.
|
||||
3
ABasicCalculator/Integers/lesson-info.yaml
Normal file
3
ABasicCalculator/Integers/lesson-info.yaml
Normal file
@@ -0,0 +1,3 @@
|
||||
content:
|
||||
- Theory
|
||||
- Task
|
||||
1
ABasicCalculator/Integers/lesson-remote-info.yaml
Normal file
1
ABasicCalculator/Integers/lesson-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 2091772948
|
||||
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "task_calculator_intro"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@@ -0,0 +1,4 @@
|
||||
pub fn intro() -> &'static str {
|
||||
// TODO: fix me 👇
|
||||
"I'm ready to insert here missing part!"
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
type: edu
|
||||
custom_name: A Basic Calculator - Introduction
|
||||
files:
|
||||
- name: src/lib.rs
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 74
|
||||
length: 24
|
||||
placeholder_text: insert here missing part
|
||||
initial_state:
|
||||
length: 24
|
||||
offset: 74
|
||||
initialized_from_dependency: false
|
||||
encrypted_possible_answer: p3YK31DNwkPk7JQv1wH1uCEizKYwTqJR03+cEeW8LH4=
|
||||
selected: false
|
||||
status: Unchecked
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
- name: tests/tests.rs
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Unchecked
|
||||
record: -1
|
||||
@@ -0,0 +1 @@
|
||||
id: 940025367
|
||||
@@ -0,0 +1,22 @@
|
||||
# A Basic Calculator
|
||||
|
||||
In this section we'll learn how to use Rust as a **calculator**.\
|
||||
It might not sound like much, but it'll give us a chance to cover a lot of Rust's basics, such as:
|
||||
|
||||
- How to define and call functions
|
||||
- How to declare and use variables
|
||||
- Primitive types (integers and booleans)
|
||||
- Arithmetic operators (including overflow and underflow behavior)
|
||||
- Comparison operators
|
||||
- Control flow
|
||||
- Panics
|
||||
|
||||
Nailing the basics with a few exercises will get the language flowing under your fingers.
|
||||
When we move on to more complex topics, such as traits and ownership, you'll be able to focus on the new concepts
|
||||
without getting bogged down by the syntax or other trivial details.
|
||||
|
||||
<br/>
|
||||
|
||||
## Task
|
||||
Introduction task is to complete the `intro` function.
|
||||
The function should return the string: ***I'm ready to build a calculator in Rust!***
|
||||
2
ABasicCalculator/Introduction/lesson-info.yaml
Normal file
2
ABasicCalculator/Introduction/lesson-info.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
content:
|
||||
- ABasicCalculatorIntroduction
|
||||
1
ABasicCalculator/Introduction/lesson-remote-info.yaml
Normal file
1
ABasicCalculator/Introduction/lesson-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 888485854
|
||||
4
ABasicCalculator/LoopsFor/Task/Cargo.toml
Normal file
4
ABasicCalculator/LoopsFor/Task/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "task_loops_for"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
8
ABasicCalculator/LoopsFor/Task/src/lib.rs
Normal file
8
ABasicCalculator/LoopsFor/Task/src/lib.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
// Rewrite the factorial function using a `for` loop.
|
||||
pub fn factorial(n: u32) -> u32 {
|
||||
let mut sum = 1;
|
||||
for i in 1..=n {
|
||||
sum *= i;
|
||||
}
|
||||
sum
|
||||
}
|
||||
27
ABasicCalculator/LoopsFor/Task/task-info.yaml
Normal file
27
ABasicCalculator/LoopsFor/Task/task-info.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
type: edu
|
||||
files:
|
||||
- name: src/lib.rs
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 87
|
||||
length: 74
|
||||
placeholder_text: /* TODO */
|
||||
initial_state:
|
||||
length: 10
|
||||
offset: 92
|
||||
initialized_from_dependency: false
|
||||
encrypted_possible_answer: E4RC0YxYtJ+r3XYct3/Vba0lSc2c1ujicbHBtaLiY6Dv+JMbZ+ZieR5jWv2O0Sq0e1xRAbtzWtjuylhM3Rln3MjtarbpC5noupVT80PyMn0=
|
||||
selected: true
|
||||
status: Solved
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
- name: tests/tests.rs
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Solved
|
||||
feedback:
|
||||
message: 恭喜!
|
||||
time: "Tue, 24 Feb 2026 09:15:32 UTC"
|
||||
record: -1
|
||||
1
ABasicCalculator/LoopsFor/Task/task-remote-info.yaml
Normal file
1
ABasicCalculator/LoopsFor/Task/task-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 726352232
|
||||
1
ABasicCalculator/LoopsFor/Task/task.md
Normal file
1
ABasicCalculator/LoopsFor/Task/task.md
Normal file
@@ -0,0 +1 @@
|
||||
This task requires you to rewrite the `factorial` function using a `for` loop.
|
||||
0
ABasicCalculator/LoopsFor/Task/tests/tests.rs
Normal file
0
ABasicCalculator/LoopsFor/Task/tests/tests.rs
Normal file
4
ABasicCalculator/LoopsFor/Theory/Cargo.toml
Normal file
4
ABasicCalculator/LoopsFor/Theory/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "theory_loops_for"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
3
ABasicCalculator/LoopsFor/Theory/src/main.rs
Normal file
3
ABasicCalculator/LoopsFor/Theory/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
// put your code here to launch it
|
||||
}
|
||||
11
ABasicCalculator/LoopsFor/Theory/task-info.yaml
Normal file
11
ABasicCalculator/LoopsFor/Theory/task-info.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
type: theory
|
||||
files:
|
||||
- name: src/main.rs
|
||||
visible: true
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Solved
|
||||
record: -1
|
||||
post_submission_on_open: true
|
||||
1
ABasicCalculator/LoopsFor/Theory/task-remote-info.yaml
Normal file
1
ABasicCalculator/LoopsFor/Theory/task-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 900138695
|
||||
64
ABasicCalculator/LoopsFor/Theory/task.md
Normal file
64
ABasicCalculator/LoopsFor/Theory/task.md
Normal file
@@ -0,0 +1,64 @@
|
||||
## Loops, part 2: `for`
|
||||
|
||||
Having to manually increment a counter variable is somewhat tedious. The pattern is also extremely common!\
|
||||
To make this easier, Rust provides a more concise way to iterate over a range of values: the `for` loop.
|
||||
|
||||
## The `for` loop
|
||||
|
||||
A `for` loop is a way to execute a block of code for each element in an iterator[^iterator].
|
||||
|
||||
Here's the general syntax:
|
||||
|
||||
```rust
|
||||
for <element> in <iterator> {
|
||||
// code to execute
|
||||
}
|
||||
```
|
||||
|
||||
## Ranges
|
||||
|
||||
Rust's standard library provides **range** type that can be used to iterate over a sequence of numbers[^weird-ranges].
|
||||
|
||||
For example, if we want to sum the numbers from 1 to 5:
|
||||
|
||||
```rust
|
||||
let mut sum = 0;
|
||||
for i in 1..=5 {
|
||||
sum += i;
|
||||
}
|
||||
```
|
||||
|
||||
Every time the loop runs, `i` will be assigned the next value in the range before executing the block of code.
|
||||
|
||||
There are five kinds of ranges in Rust:
|
||||
|
||||
- `1..5`: A (half-open) range. It includes all numbers from 1 to 4. It doesn't include the last value, 5.
|
||||
- `1..=5`: An inclusive range. It includes all numbers from 1 to 5. It includes the last value, 5.
|
||||
- `1..`: An open-ended range. It includes all numbers from 1 to infinity (well, until the maximum value of the integer type).
|
||||
- `..5`: A range that starts at the minimum value for the integer type and ends at 4. It doesn't include the last value, 5.
|
||||
- `..=5`: A range that starts at the minimum value for the integer type and ends at 5. It includes the last value, 5.
|
||||
|
||||
You can use a `for` loop with the first three kinds of ranges, where the starting point
|
||||
is explicitly specified. The last two range types are used in other contexts, that we'll cover later.
|
||||
|
||||
The extreme values of a range don't have to be integer literals—they can be variables or expressions too!
|
||||
|
||||
For example:
|
||||
|
||||
```rust
|
||||
let end = 5;
|
||||
let mut sum = 0;
|
||||
|
||||
for i in 1..(end + 1) {
|
||||
sum += i;
|
||||
}
|
||||
```
|
||||
|
||||
## Further reading
|
||||
|
||||
- [`for` loop documentation](https://doc.rust-lang.org/std/keyword.for.html)
|
||||
|
||||
[^iterator]: Later in the course we'll give a precise definition of what counts as an "iterator".
|
||||
For now, think of it as a sequence of values that you can loop over.
|
||||
[^weird-ranges]: You can use ranges with other types too (e.g. characters and IP addresses),
|
||||
but integers are definitely the most common case in day-to-day Rust programming.
|
||||
4
ABasicCalculator/LoopsFor/lesson-info.yaml
Normal file
4
ABasicCalculator/LoopsFor/lesson-info.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
custom_name: Loops for
|
||||
content:
|
||||
- Theory
|
||||
- Task
|
||||
1
ABasicCalculator/LoopsFor/lesson-remote-info.yaml
Normal file
1
ABasicCalculator/LoopsFor/lesson-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 315334556
|
||||
4
ABasicCalculator/LoopsWhile/Task/Cargo.toml
Normal file
4
ABasicCalculator/LoopsWhile/Task/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "task_loops_while"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
17
ABasicCalculator/LoopsWhile/Task/src/lib.rs
Normal file
17
ABasicCalculator/LoopsWhile/Task/src/lib.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
// Rewrite the factorial function using a `while` loop.
|
||||
pub fn factorial(n: u32) -> u32 {
|
||||
// The `todo!()` macro is a placeholder that the compiler
|
||||
// interprets as "I'll get back to this later", thus
|
||||
// suppressing type errors.
|
||||
// It panics at runtime.
|
||||
let mut i = 1;
|
||||
let mut sum = 1;
|
||||
|
||||
while i <= n {
|
||||
sum *= i;
|
||||
i += 1;
|
||||
}
|
||||
sum
|
||||
|
||||
|
||||
}
|
||||
27
ABasicCalculator/LoopsWhile/Task/task-info.yaml
Normal file
27
ABasicCalculator/LoopsWhile/Task/task-info.yaml
Normal file
@@ -0,0 +1,27 @@
|
||||
type: edu
|
||||
files:
|
||||
- name: src/lib.rs
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 269
|
||||
length: 110
|
||||
placeholder_text: todo!()
|
||||
initial_state:
|
||||
length: 7
|
||||
offset: 274
|
||||
initialized_from_dependency: false
|
||||
encrypted_possible_answer: E4RC0YxYtJ+r3XYct3/VbWhv0KY/DdVS2mEavS7NAeplIUze6rifYlz1/wF5ymecNIoK2ZQBm5WZbDVHNj0JjOi99JvSWj2HBmI6fZkCA4iDtsO00y8H7ja3yZio8lULMTOz/X12UEgThCYMWyBCTQ==
|
||||
selected: true
|
||||
status: Solved
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
- name: tests/tests.rs
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Solved
|
||||
feedback:
|
||||
message: 恭喜!
|
||||
time: "Tue, 24 Feb 2026 09:10:50 UTC"
|
||||
record: -1
|
||||
1
ABasicCalculator/LoopsWhile/Task/task-remote-info.yaml
Normal file
1
ABasicCalculator/LoopsWhile/Task/task-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 1557849382
|
||||
2
ABasicCalculator/LoopsWhile/Task/task.md
Normal file
2
ABasicCalculator/LoopsWhile/Task/task.md
Normal file
@@ -0,0 +1,2 @@
|
||||
This task requires you to rewrite the `factorial` function.
|
||||
Previously, the `factorial` function was likely implemented using recursion. In this iteration, your goal is to reimplement the same functionality but using a `while` loop instead.
|
||||
0
ABasicCalculator/LoopsWhile/Task/tests/tests.rs
Normal file
0
ABasicCalculator/LoopsWhile/Task/tests/tests.rs
Normal file
4
ABasicCalculator/LoopsWhile/Theory/Cargo.toml
Normal file
4
ABasicCalculator/LoopsWhile/Theory/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "theory_loops_while"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
3
ABasicCalculator/LoopsWhile/Theory/src/main.rs
Normal file
3
ABasicCalculator/LoopsWhile/Theory/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
// put your code here to launch it
|
||||
}
|
||||
11
ABasicCalculator/LoopsWhile/Theory/task-info.yaml
Normal file
11
ABasicCalculator/LoopsWhile/Theory/task-info.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
type: theory
|
||||
files:
|
||||
- name: src/main.rs
|
||||
visible: true
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
status: Solved
|
||||
record: -1
|
||||
post_submission_on_open: true
|
||||
1
ABasicCalculator/LoopsWhile/Theory/task-remote-info.yaml
Normal file
1
ABasicCalculator/LoopsWhile/Theory/task-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 99013028
|
||||
85
ABasicCalculator/LoopsWhile/Theory/task.md
Normal file
85
ABasicCalculator/LoopsWhile/Theory/task.md
Normal file
@@ -0,0 +1,85 @@
|
||||
## Loops, part 1: `while`
|
||||
|
||||
Your implementation of `factorial` has been forced to use recursion.\
|
||||
This may feel natural to you, especially if you're coming from a functional programming background.
|
||||
Or it may feel strange, if you're used to more imperative languages like C or Python.
|
||||
|
||||
Let's see how you can implement the same functionality using a **loop** instead.
|
||||
|
||||
## The `while` loop
|
||||
|
||||
A `while` loop is a way to execute a block of code as long as a **condition** is true.\
|
||||
Here's the general syntax:
|
||||
|
||||
```rust
|
||||
while <condition> {
|
||||
// code to execute
|
||||
}
|
||||
```
|
||||
|
||||
For example, we might want to sum the numbers from 1 to 5:
|
||||
|
||||
```rust
|
||||
let sum = 0;
|
||||
let i = 1;
|
||||
// "while i is less than or equal to 5"
|
||||
while i <= 5 {
|
||||
// `+=` is a shorthand for `sum = sum + i`
|
||||
sum += i;
|
||||
i += 1;
|
||||
}
|
||||
```
|
||||
|
||||
This will keep adding 1 to `i` and `i` to `sum` until `i` is no longer less than or equal to 5.
|
||||
|
||||
## The `mut` keyword
|
||||
|
||||
The example above won't compile as is. You'll get an error like:
|
||||
|
||||
```text
|
||||
error[E0384]: cannot assign twice to immutable variable `sum`
|
||||
--> src/main.rs:7:9
|
||||
|
|
||||
2 | let sum = 0;
|
||||
| ---
|
||||
| |
|
||||
| first assignment to `sum`
|
||||
| help: consider making this binding mutable: `mut sum`
|
||||
...
|
||||
7 | sum += i;
|
||||
| ^^^^^^^^ cannot assign twice to immutable variable
|
||||
|
||||
error[E0384]: cannot assign twice to immutable variable `i`
|
||||
--> src/main.rs:8:9
|
||||
|
|
||||
3 | let i = 1;
|
||||
| -
|
||||
| |
|
||||
| first assignment to `i`
|
||||
| help: consider making this binding mutable: `mut i`
|
||||
...
|
||||
8 | i += 1;
|
||||
| ^^^^^^ cannot assign twice to immutable variable
|
||||
```
|
||||
|
||||
This is because variables in Rust are **immutable** by default.\
|
||||
You can't change their value once it has been assigned.
|
||||
|
||||
If you want to allow modifications, you have to declare the variable as **mutable** using the `mut` keyword:
|
||||
|
||||
```rust
|
||||
// `sum` and `i` are mutable now!
|
||||
let mut sum = 0;
|
||||
let mut i = 1;
|
||||
|
||||
while i <= 5 {
|
||||
sum += i;
|
||||
i += 1;
|
||||
}
|
||||
```
|
||||
|
||||
This will compile and run without errors.
|
||||
|
||||
## Further reading
|
||||
|
||||
- [`while` loop documentation](https://doc.rust-lang.org/std/keyword.while.html)
|
||||
4
ABasicCalculator/LoopsWhile/lesson-info.yaml
Normal file
4
ABasicCalculator/LoopsWhile/lesson-info.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
custom_name: Loops while
|
||||
content:
|
||||
- Theory
|
||||
- Task
|
||||
1
ABasicCalculator/LoopsWhile/lesson-remote-info.yaml
Normal file
1
ABasicCalculator/LoopsWhile/lesson-remote-info.yaml
Normal file
@@ -0,0 +1 @@
|
||||
id: 178597809
|
||||
4
ABasicCalculator/OverflowAndUnderflow/Task/Cargo.toml
Normal file
4
ABasicCalculator/OverflowAndUnderflow/Task/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "task_overflow_and_underflow"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
8
ABasicCalculator/OverflowAndUnderflow/Task/src/lib.rs
Normal file
8
ABasicCalculator/OverflowAndUnderflow/Task/src/lib.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
pub fn factorial(n: u32) -> u32 {
|
||||
let mut result: u32 = 1;
|
||||
for i in 1..=n {
|
||||
result = result.wrapping_mul(i);
|
||||
/* TODO: Use wrapping_mul to ensure multiplication wraps around on overflow */
|
||||
}
|
||||
result
|
||||
}
|
||||
28
ABasicCalculator/OverflowAndUnderflow/Task/task-info.yaml
Normal file
28
ABasicCalculator/OverflowAndUnderflow/Task/task-info.yaml
Normal file
@@ -0,0 +1,28 @@
|
||||
type: edu
|
||||
files:
|
||||
- name: src/lib.rs
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 133
|
||||
length: 78
|
||||
placeholder_text: "/* TODO: Use wrapping_mul to ensure multiplication wraps around\
|
||||
\ on overflow */"
|
||||
initial_state:
|
||||
length: 78
|
||||
offset: 92
|
||||
initialized_from_dependency: false
|
||||
encrypted_possible_answer: zfLqC5JecSQrCUqhJOpCYXzRrrFwjAUqLYWmi8ftlzbNFqwi8fYglAnuG8NTy6M+
|
||||
selected: true
|
||||
status: Solved
|
||||
learner_created: false
|
||||
- name: Cargo.toml
|
||||
visible: false
|
||||
learner_created: false
|
||||
- name: tests/tests.rs
|
||||
visible: true
|
||||
learner_created: false
|
||||
status: Solved
|
||||
feedback:
|
||||
message: 恭喜!
|
||||
time: "Tue, 24 Feb 2026 09:32:03 UTC"
|
||||
record: -1
|
||||
@@ -0,0 +1 @@
|
||||
id: 1321002669
|
||||
1
ABasicCalculator/OverflowAndUnderflow/Task/task.md
Normal file
1
ABasicCalculator/OverflowAndUnderflow/Task/task.md
Normal file
@@ -0,0 +1 @@
|
||||
Adjust the `factorial` function so arithmetic overflows wrap around, matching the `twentieth()` test case's expected result for `factorial(20)`.
|
||||
34
ABasicCalculator/OverflowAndUnderflow/Task/tests/tests.rs
Normal file
34
ABasicCalculator/OverflowAndUnderflow/Task/tests/tests.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use task_overflow_and_underflow::factorial;
|
||||
|
||||
#[test]
|
||||
fn twentieth() {
|
||||
// 20! is 2432902008176640000, which is too large to fit in a u32
|
||||
// With the default dev profile, this will panic when you run `cargo test`
|
||||
// We want it to wrap around instead
|
||||
assert_eq!(factorial(20), 2_192_834_560);
|
||||
// ☝️
|
||||
// A large number literal using underscores to improve readability!
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn first() {
|
||||
assert_eq!(factorial(0), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn second() {
|
||||
assert_eq!(factorial(1), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn third() {
|
||||
assert_eq!(factorial(2), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fifth() {
|
||||
assert_eq!(factorial(5), 120);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user